cmdlet指令
"什麼是cmdlet指令?
" 舉例:Get-Service、New-Item、Get-Process、Stop-Process 等 "cmdlets是Powershell的内部指令,
每個指令有一個動詞和名詞組成。" 指令
"如何檢視所有cmdlet指令?
(用什麼、如周遊程序、檔案)" get-command 指令
"查閱某條指令的摘要、參數?
(怎麼用、比如傳什麼參)" "舉例:get-process -?
或者:get-help Get-Process
或者:man get-process
或者:help get-process
或者:get-help get-process -online" "使用幫助參數""-?""
想要查詢的指令後面加""-?參數""後執行就可以了。
或者去官方手冊、IDE中查詢。" 指令
指令的參數種類有幾種? "标準參數:
幫助參數:-?
通用參數:由ps的引擎進行控制,行為方式相同
建議參數:不是強制的,但存在明确的用法指南以鼓勵标準化" 指令
查閱對象的屬性和方法? 舉例:get-service
get-member 指令
列出特定對象的屬性值? 舉例:(get-service alerter).canpauseandcontinue 指令
調用特定對象的方法? 舉例:(get-service schedule).stop() 指令
如何設定輸出過濾? get-service
get-member -membertype property "get-service是擷取所有對象。
get-member是擷取這些對象有哪些方法和屬性。
-membertype property是過濾作用,
隻顯示membertype屬性為property的對象。" 指令
"格式化輸出?
format" get-service
format-list "預設輸出,隻會選擇輸出一部分,可以使用format系列指令定制輸出樣式
四種格式
Format-List
Format-Custom
Format-Table
Format-Wide" 格式
查詢cmdlet對應别名? get-alias
where-object {$_.definition -eq "set-location"} "get-alias
where-object {$_.definition -eq ""set-location""}
get-alias 周遊所有{指令别名的對象}
where-object 會對集合元素逐個過濾,将符合條件的結果保留
definition 是對象的屬性之一
eq 求相同的" 别名
查詢别名對應的cmdlet? get-alias ps 别名
如何建立新的别名? set-alias gh get-help 别名
如何删除新的别名? remove-item alias:ls 别名
如何用函數建立替代名稱? function bootini {notepad c:\boot.ini} "因為别名沒法帶參數,用函數就可以解決這個問題了
使用記事本打開 Boot.ini 檔案" 别名
對象優先級 "Alias(1)
Function(2)
Filter(2)
Cmdlet(3)
Application(4)
ExternalScript(5)
Script (-)" 同名對象的執行優先級 優先級
突破優先級,執行指定對象 "CommandType Name
Alias Ping
Function Ping
Application PING.EXE
PS E:> $command=Get-Command -Name ping | where {$_.CommandType -eq ""Application"" }
PS E:> & $command" 優先級
調用操作符 "$command = ""Dir""
$command
& $command
" "調用操作符“&”
調用操作符隻能接受單個指令(可以使用語句塊解除)" 腳本塊
腳本塊 "$i = {""目前時間:"" + (get-date) }
& $i" "使用大括号定義,
可以使用之前的調用操作符“&”執行腳本塊。" 腳本塊
給腳本塊傳遞參數 & { param($people="everyone") write-host "Hello, $people ” } "Mosser" "定義和傳遞參數一次性完成,
可以了解為匿名函數
函數本質是一個已命名的腳本塊" 腳本塊
Invoke-Expression Invoke-Expression 'Get-Process | Where-Object { $_.Name -like "e*"}' "邏輯為:将一條字元串傳遞給 調用操作符。
單引号可以防止變量被替換" 腳本塊
foreach-object腳本塊 Get-Process | ForEach-Object { $.name } 管道中的foreach-object本身後面也會帶語句塊,針對數組中的每一個元素分别傳遞給 調用操作符 處理 腳本塊
Begin, Process, End 管道腳本塊 "get-process | select -last 5 | &{
begin {""開始準備環境""}
process{$.Name}
end{""開始清理環境""}
}" "開始準備環境
WmiPrvSE
Wox
WUDFHost
ZhuDongFangYu
開始清理環境" 腳本塊
自動化變量$ExecutionContext "PS E:> $ExecutionContext
Host:System.Management.Automation.Internal.Host.InternalHost
Events: System.Management.Automation.PSLocalEventManager
InvokeProvider:System.Management.Automation.ProviderIntrinsics
SessionState:System.Management.Automation.SessionState
InvokeCommand:System.Management.Automation.CommandInvocationIntrinsics" "上下文
ExecutionContext"
定義 特殊字元 内部方法 "上下文
處理字元串中的變量 “ ExpandString() "上下文
執行指令集 & InvokeScript() "上下文
建立一個新的代碼塊 {} NewScriptBlock() "上下文
"$ExecutionContext.
InvokeCommand
處理變量" "cls
$site = '飛苔部落格'
$text1 = ""test1 我的個人網站 $site""
$text2 = 'test2 我的個人網站 $site'
$text1
$text2
$executioncontext.InvokeCommand.ExpandString($text2)
test1 我的個人網站 飛苔部落格
test2 我的個人網站 $site
test2 我的個人網站 飛苔部落格
PS C:\Users\int3b\Documents\ps_test> " "雙引号的機制是ExpandString()方法,
将變量替換成變量本身的内容。" "上下文
建立腳本塊" "$block = {
$write=Get-Process QQProtect;
""$($write.Name) 占用記憶體: $($write.WorkingSet/1mb) MB""
}
$block.GetType().Name
& $block
$blockStr='$write=Get-Process QQProtect;""$($write.Name) 占用記憶體: $($write.WorkingSet/1mb) MB""'
$block = $executioncontext.InvokeCommand.NewScriptBlock($blockStr)
& $block" "ScriptBlock
QQProtect 占用記憶體: 10.078125 MB
ScriptBlock
QQProtect 占用記憶體: 10.078125 MB" "上下文
執行指令行" "$cmd='333.14'
& { 333.14}
$executioncontext.InvokeCommand.InvokeScript($cmd)
Invoke-Expression $cmd" "28.26
28.26
28.26" "上下文
SessionState
管理所有變量" "$value = ""Test""
$executioncontext.SessionState.PSVariable.GetValue(""value"")
$executioncontext.SessionState.PSVariable.Set(""value"", 100)
$value" "SessionState是一個用來表現Powershell環境的對象,
PSVariable,可以取出和更新Powershell中所有的變量." "上下文
檢視驅動器" "PS E:> $executioncontext.SessionState.Drive.Current
PS E:> $executioncontext.SessionState.Drive.GetAll()
PS E:> $executioncontext.SessionState.Drive.GetAllForProvider(""FileSystem"")" "上下文