Powershell基礎知識點
1、Powershell指令檔的檔案名稱以.PS1擴充(注意是數位1)
2、Powershell執行策略
預設為Restricted,可以通過下面的cmdlet命令擷取當前執行策略:
Get-ExecutionPolicy
可選的執行策略有如下:
Restricted - 指令碼不能運行
RemoteSigned - 本地建立的指令碼可以運行,但從網上下載的指令碼不能運行(除非它們擁有由信任的發行者簽署的數位簽章)
AllSigned - 僅當指令碼由信任的發行者簽名才能運行
Unrestricted - 指令碼執行不受限制,不管來自哪裡,也不管它們是否有簽名
可以用下面的cmdlet命令重新設定執行策略:
Set-ExecutionPolicy <policy name>
3、運行指令碼
需要鍵入指令碼的完整路徑,如d:\scripts\script.psi
如果指令檔剛好位於系統目錄中,可以鍵入 .\script.psi 運行
注意 .\ 不能省
4、管道
Get-Process | Sort-Object ID
擷取進程列表並按進程ID排序
5、變數
$a = Get-Process
$b = (Get-Process | Sort-Object ID)
6、@符號
使用@符號可以把列表內容轉換成數組,比如下面:
$procs = @{name="explorer", "taskmgr"}
Get-Process @procs
第二行加@確保procs被當做數組處理
7、split
"This is a test" -split " "
8、join
"Hello","world" -join ","
9、斷點
根據行號插入斷點:
New-PSBreakpint -Script d:\Scripts\Script.ps1 -Line 10
即在指令碼的第10行插入一個斷點
也可以將斷點綁定到變數上:
New-PSBreakpoint -Script d:\Scripts\Script.ps1 -variables a
其他命令包括:
Get-PSBreakpoint
Enable-PSBreakpoint
Disable-PSBreakpoint
Remove-PSBreakpoint
10、單步調試
Step-Into
Step-Out