自動(dòng)化具有高度方便的特點(diǎn),同時(shí)也可能會(huì)自動(dòng)產(chǎn)生一些不可避免的錯(cuò)誤。這也就是Powershell為什么會(huì)有一些專門(mén)來(lái)防止和處理危險(xiǎn)的機(jī)制:這些機(jī)制會(huì)對(duì)接下來(lái)要執(zhí)行的操作進(jìn)行確認(rèn)。
試運(yùn)行:模擬操作
如果你想知道一個(gè)確定的命令會(huì)產(chǎn)生什么影響,你可以進(jìn)行試運(yùn)行。這時(shí),Powershell不會(huì)執(zhí)行任何對(duì)系統(tǒng)有影響的操作,只會(huì)告訴你如果沒(méi)有模擬運(yùn)行,可能產(chǎn)生什么影響和后果。通過(guò)-whatif 參數(shù)。事實(shí)上,許多cmdltes都支持試運(yùn)行。
#如果執(zhí)行stop-process -name *a*會(huì)終止下面的進(jìn)程。Stop-Process -Name *a* -WhatIfWhatIf: 對(duì)目標(biāo)“AcroRd32 (4544)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“AcroRd32 (4836)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“Alipaybsm (2888)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“AlipaySafeTran (2808)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“AlipaySecSvc (1656)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“AliveService (1684)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“CBGrabConnect_x64 (6052)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“FlashUtil32_11_2_202_235_ActiveX (1136)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“IAStorDataMgrSvc (2920)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“IAStorIcon (3112)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“l(fā)sass (740)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“notepad++ (5124)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“RAVCpl64 (3484)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“SearchIndexer (3788)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“taskhost (2304)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“unsecapp (264)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“unsecapp (2680)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“wlanext (1420)”執(zhí)行操作“Stop-Process”。WhatIf: 對(duì)目標(biāo)“ZhuDongFangYu (1156)”執(zhí)行操作“Stop-Process”。
當(dāng)然如果你想讓自己的腳本和函數(shù)也支持模擬運(yùn)行,只需要進(jìn)行簡(jiǎn)單的整合。多增加一個(gè)switch參數(shù)。
function MapDrive([string]$driveletter, [string]$target, [switch]$whatif){ If ($whatif) { Write-Host "WhatIf: creation of a network drive " + "with the letter ${driveletter}: at destination $target" } Else { New-PSDrive $driveletter FileSystem $target }}# 首先進(jìn)行模擬運(yùn)行:MapDrive k 127.0.0.1c$ -whatifWhatIf: creation of a network drivewith letter k: at destination 127.0.0.1c$ # 執(zhí)行命令MapDrive k 127.0.0.1c$Name Provider Root---- -------- ----k FileSystem 127.0.0.1c$
逐步確認(rèn):逐個(gè)查詢
正如之前看到的那樣,在使用“*”通配符時(shí),可能會(huì)在瞬間產(chǎn)生許多任務(wù)。為了防止產(chǎn)生失誤操作,可以逐個(gè)進(jìn)行確認(rèn),逐個(gè)進(jìn)行放行。
Stop-Process -Name *cm* -Confirm 確認(rèn)是否確實(shí)要執(zhí)行此操作?對(duì)目標(biāo)“cmd (1012)”執(zhí)行操作“Stop-Process”。[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 掛起(S) [?] 幫助 (默認(rèn)值為“Y”): ?Y - 僅繼續(xù)執(zhí)行操作的下一步驟。A - 繼續(xù)執(zhí)行操作的所有步驟。N - 跳過(guò)此操作并繼續(xù)執(zhí)行下一操作。L - 跳過(guò)此操作及所有后續(xù)操作。S - 暫停當(dāng)前管道并返回到命令提示符。鍵入“exit”可繼續(xù)執(zhí)行該管道。[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 掛起(S) [?] 幫助 (默認(rèn)值為“Y”): N 確認(rèn)是否確實(shí)要執(zhí)行此操作?對(duì)目標(biāo)“cmd (3704)”執(zhí)行操作“Stop-Process”。[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 掛起(S) [?] 幫助 (默認(rèn)值為“Y”): sPS E:>>>PS E:>>> exit 確認(rèn)是否確實(shí)要執(zhí)行此操作?對(duì)目標(biāo)“cmd (3704)”執(zhí)行操作“Stop-Process”。[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 掛起(S) [?] 幫助 (默認(rèn)值為“Y”): y 確認(rèn)是否確實(shí)要執(zhí)行此操作?對(duì)目標(biāo)“cmd (4404)”執(zhí)行操作“Stop-Process”。[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 掛起(S) [?] 幫助 (默認(rèn)值為“Y”): a
新聞熱點(diǎn)
疑難解答
圖片精選