- 執行 PowerShell Script
- 在 PowewrShell環境中執行script
- PS C:\> full file path(.ps1)
- 一般是輸入script完整路徑
- 若script位於$PSHOME中 則輸入script名稱即可
- PS C:\> full file path(.ps1)
- 檔案路徑有空白
- 執行時需輸入 &"檔案路徑"
- PS C:\> &"C:\my script\test.ps1"
- 如果沒有用&, 則會將雙引號中的內容認為是字串顯示 而非執行雙引號中的script
- 執行時需輸入 &"檔案路徑"
- 在 PowewrShell環境中執行script
- 開始->執行的 開啟 中 輸入
- powershell.exe -noexit 完整檔案路徑
- powershell.exe : powershell 的執行檔
- -noexit : 執行後保留執行視窗
- powershell.exe : powershell 的執行檔
- powershell.exe -noexit 完整檔案路徑
- 在powershell環境中執行vbscript
- cscript test.vbs
- 命令列無法取用到script中的變數值
- cscript test.vbs
- . test.ps1
- 命令列可以取用到script中的變數值
- 基本
- 換行(breaking lines)
- ` (空一個空白再加上1左邊那個鍵,不是單引號鍵)
- | (若有使用到pipeline 則可利用其作為換行的地方)
- get-command |
get-member
- get-command |
- ` (空一個空白再加上1左邊那個鍵,不是單引號鍵)
- Varialbes and constants
- $a = 5 ; $a = "Hey" ; $a = get-date #變數一律以$開頭
- [int] $a = 5 #指定變數型態
- Data Type
- 常數變數(constants)
- PS C:\> set-variable -name PI -value 3.1415 -option constant #不用$
PS C:\> $PI =3.14 #'嘗試修改常數變數 - 錯誤訊息如下
Cannot overwrite variable PI because it is read-only or constant.
At line:1 char:4
+ $PI <<<< =3.14 - 移除變數
- Remove-variable PI
- 顯示資料於螢幕上(Echoing data)
- PS C:\>$a #直接於命令列中數入欲檢視的變數
- PS C\>write-host $a #使用cmdelt
- 布林值(boolean)
- $a= $True
- $b= $False
- $c= $Null
- 註解 #
- 單行執行多指令時,指令間的分隔符號(Seperating commands)
- $a=1,2,3,4,5 ; $a[2]
- 陣列(Arrays)
- $a=1,2,3,4,5 #用逗號(,)分隔陣列的元素
- $a += 6 #增加元素
- Assignment operators
- ++
- $a++ 表示 $a = $a +1
- --
- $a-- 表示 $a = $a -1
- -=
- $a-=3 表示 $a = $a -3
- +=
- $a+=3 表示 $a = $a +3
- 讓系統發出嗶一聲(Insert a Beep)
- write-Warning "Error `a"
- PS C:\> Write-Warning "Error! `a"
WARNING: Error! - 播放wav聲音檔案
- $a = new-object -type system.media.soundplayer
- $a.SoundLocation = "C:\wondows\media\ringin.wav" #指定檔案路徑
- $a.Play() #播放
- 可用$a | get-member看有什麼屬性 方法可以使用
- 取物件集合(Retrieve a collection)
- $a = get-childitem "c:\scripts" # $a 為物件參考
- $a.length #共有多少物件
- 呼叫方法(Calling Methods)
- $a.GetType()
- 若為$a.GetType 則顯示該方法知定義 而非執行該方法
- Foreach
- foreach ($i in get-childitem c:\){$i.extension}
- Do...While
- $a = 1 ; do{$a;$a++}while($a -lt 10) #列出1~9;while為False就跳出
- Do...Until
- $a = 1 ; do{$a;$a++}until($a -gt 10) #列出1~10;until為True就跳出
- For Next Loop
- for($a =1 ; $a -le 10; $a++) {$a} #列出1~10
- $a = "white"
if ($a -eq "red")
{"The color is red."}
elseif ($a -eq "white")
{"The color is white."}
else
{"The color is blue."}
- $a = 5
switch ($a)
{
1 {"The color is red."}
2 {"The color is blue."}
3 {"The color is green."}
4 {"The color is yellow."}
5 {"The color is orange."}
6 {"The color is purple."}
7 {"The color is pink."}
8 {"The color is brown."}
default {"The color could not be determined."}
}
- 從命令列輸入之參數(Command-Line Arguments)
- test.ps1 A B C D #讀取值
- test.ps1 $a #讀取變數
- 讀取方式:
- foreach($i in $args){$i}
- $args[0],$args[1],$args[2]
- 從檔案讀取參數
- $a = get-content "檔案完整路徑"
- foreach($i in $a){$i}
- 從螢幕輸入
- $a = read-host "Please input your name:"
- 輸出到螢幕
- 輸出到檔案
- get-service | out-file "完整檔案路徑"
- get-service > "完整檔案路徑" #若檔案存在 則覆蓋該檔案
- get-service >> "完整檔案路徑" #若檔案存在 則將資料附加到該檔案
- get-service | tee-object "完整檔案路徑" #結果會同時顯示於螢幕
全站熱搜