循环部分的脚本

时间:2014-07-31 17:39:04

标签: loops powershell

mode con: cols=52 lines=16
while ($script -ne "Q") {
$tag = ""
while (-not ($tag)) {
$tag = Read-Host 'Enter tag # or Q to quit'
$date = get-date -f MM-dd-yyyy_HH_mm_ss
$username = [Environment]::Username 

if(!(Test-Path -path "C:\Users\$username\Desktop\PSTools\Screenshot Dump\")) {
New-Item "C:\Users\$username\Desktop\PSTools\Screenshot Dump\" -type directory
}

}

if ($tag -eq "Q"){break}

cls

#$ErrorActionPreference = 'silentlycontinue'

cd "C:\Users\$username\Desktop\PSTools"

set-alias psexec "C:\Users\$username\Desktop\PSTools\PsExec.exe"

set-alias nircmd "C:\Users\$username\Desktop\PSTools\nircmd.exe"

------>LOOP START HERE

psexec \\$tag -i -c -s nircmd savescreenshotfull "C:\$tag-$date.png"

move "\\$tag\c$\$tag-$date.png" "C:\Users\$username\Desktop\PSTools\Screenshot Dump\$tag-$date.png"

explorer "C:\Users\$username\Desktop\PSTools\Screenshot Dump\$tag-$date.png"

------>LOOP END HERE

"`n"
}

基本上我试图循环我上面说过的地方,但我习惯在命令提示符下执行它并且它不一样。我想让它循环X次,或直到我按CTRL + C退出它。

2 个答案:

答案 0 :(得分:2)

您也可以使用Foreach循环。 让我们说你想循环5次:

$NumberOfLoops = 5
Foreach ($loop in (1..$NumberOfLoops)) { Do Loopy Stuff }

答案 1 :(得分:1)

您可以使用基本FOR语句循环。虽然,PowerShell有一些很棒的循环技术。值得一看......

http://social.technet.microsoft.com/wiki/contents/articles/4542.powershell-loops.aspx

对于基本FOR循环的示例,只需这样做(将循环10次($ x = 0-9)):

FOR ($x=0; $x -lt 10; $x++) { DO LOOPY STUFF };