有没有办法让脚本跳转到特定的位置,如:命令提示符中的GOTO?我希望脚本在结束时跳转到开头.
$tag1 = Read-Host 'Enter tag #'
cls
sc.exe \\$tag1 start RemoteRegistry
cls
Start-Sleep -s 2
cls
systeminfo /S $tag1 | findstr /B /C:"OS Name" /C:"System Boot Time" /C:"System Up Time";
Get-EventLog system -computername $tag1 -InstanceId 2147489657 -Newest 10 | ft EventID,TimeWritten,MachineName -AutoSize
Pause
Run Code Online (Sandbox Code Playgroud)
mjo*_*nor 12
以下是使用脚本的示例:
$GetInfo = {
$tag1 = Read-Host 'Enter tag # or Q to quit'
if ($tag1 -eq 'Q'){Return}
cls
sc.exe \\$tag1 start RemoteRegistry
cls
Start-Sleep -s 2
cls
systeminfo /S $tag1 | findstr /B /C:"OS Name" /C:"System Boot Time" /C:"System Up Time"
Get-EventLog system -computername $tag1 -InstanceId 2147489657 -Newest 10 |
ft EventID,TimeWritten,MachineName -AutoSize
.$GetInfo
}
&$GetInfo
Run Code Online (Sandbox Code Playgroud)
使用 .而不是脚本块中的&,以防止它向上走调用堆栈.
将代码放入脚本块中以便稍后从脚本中的任意点调用(模拟GoTo)在功能上与使用函数相同,并且以这种方式使用的脚本块有时被称为"匿名函数".
PowerShell中没有goto,也没有人错过它:).只需将命令块包装在一个循环或其他东西中.
或者尝试以下.您可以将命令列表分配给变量,然后使用执行它们&$varname
.不过,它仍然没有转到.
$commands = {
Write-Host "do some work"
$again = Read-Host "again?"
if ($again -eq "y"){
&$commands
} else {
Write-Host "end"
}
}
&$commands
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
60479 次 |
最近记录: |