Loops 每x秒循环一次脚本
我可以在我的代码周围放些什么使它每x秒自动循环一次,但是如果脚本被CTRL+C取消,让它再次请求输入Loops 每x秒循环一次脚本,loops,powershell,Loops,Powershell,我可以在我的代码周围放些什么使它每x秒自动循环一次,但是如果脚本被CTRL+C取消,让它再次请求输入 mode con: cols=40 lines=5 while ($tag -ne "Q"){ $tag1 = "" while (-not ($tag1)) { $tag1 = Read-Host 'Enter tag #, IP, or Q to quit' } if($tag1 -eq "Q"){break} $ErrorA
mode con: cols=40 lines=5
while ($tag -ne "Q"){
$tag1 = ""
while (-not ($tag1)) {
$tag1 = Read-Host 'Enter tag #, IP, or Q to quit'
}
if($tag1 -eq "Q"){break}
$ErrorActionPreference = 'silentlycontinue'
mode con: cols=80 lines=53
cls
sc.exe \\$tag1 start RemoteRegistry;
cls
start-sleep -seconds 2
*-Need Loop here-*
cls
$CompInfo = get-wmiobject -class win32_computersystem -computername $tag1;
$username = $CompInfo.UserName.Split("\")[1]; #Outputs DOMAIN\USER into an array and selects USER to display
$fullname = ((net user $username /domain | Select-String "Full Name") -replace "Full Name","").Trim(); #Takes the USER and displays the full name
#get-wmiobject -class win32_computersystem -computername c73118 | format-table -Property @{Name="DOMAIN\user";Expression={$_.username}} --> Get w/o variable
$CompInfo `
| Format-Table -Autosize -Property `
@{ Name = "DOMAIN\user"; Expression = { $_.username } },
@{ Name = "Full Name"; Expression = { $fullname } };
}
方法是将其拆分为两个脚本。让第一个脚本输入计算机名,第二个脚本循环返回登录用户 您的代码如下所示: C:\test\Script1.ps1
mode con: cols=40 lines=5
while ($tag -ne "Q"){
$tag1 = ""
while (-not ($tag1)) {
$tag1 = Read-Host 'Enter tag #, IP, or Q to quit'
}
if($tag1 -eq "Q"){break}
$ErrorActionPreference = 'silentlycontinue'
mode con: cols=80 lines=53
cls
sc.exe \\$tag1 start RemoteRegistry;
cls
start-sleep -seconds 2
#Launch Second script
Start-Process powershell.exe -ArgumentList "-NoProfile -Command `"& C:\Test\Script2.ps1 $tag1`" "
}
$tag1 = $args[0]
While(1)
{
$CompInfo = get-wmiobject -class win32_computersystem -computername $tag1;
$username = $CompInfo.UserName.Split("\")[1]; #Outputs DOMAIN\USER into an array and selects USER to display
$fullname = ((net user $username /domain | Select-String "Full Name") -replace "Full Name","").Trim(); #Takes the USER and displays the full name
#get-wmiobject -class win32_computersystem -computername c73118 | format-table -Property @{Name="DOMAIN\user";Expression={$_.username}} --> Get w/o variable
$CompInfo `
| Format-Table -Autosize -Property `
@{ Name = "DOMAIN\user"; Expression = { $_.username } },
@{ Name = "Full Name"; Expression = { $fullname } };
Start-Sleep -Seconds 2
}
C:\test\Script2.ps1
mode con: cols=40 lines=5
while ($tag -ne "Q"){
$tag1 = ""
while (-not ($tag1)) {
$tag1 = Read-Host 'Enter tag #, IP, or Q to quit'
}
if($tag1 -eq "Q"){break}
$ErrorActionPreference = 'silentlycontinue'
mode con: cols=80 lines=53
cls
sc.exe \\$tag1 start RemoteRegistry;
cls
start-sleep -seconds 2
#Launch Second script
Start-Process powershell.exe -ArgumentList "-NoProfile -Command `"& C:\Test\Script2.ps1 $tag1`" "
}
$tag1 = $args[0]
While(1)
{
$CompInfo = get-wmiobject -class win32_computersystem -computername $tag1;
$username = $CompInfo.UserName.Split("\")[1]; #Outputs DOMAIN\USER into an array and selects USER to display
$fullname = ((net user $username /domain | Select-String "Full Name") -replace "Full Name","").Trim(); #Takes the USER and displays the full name
#get-wmiobject -class win32_computersystem -computername c73118 | format-table -Property @{Name="DOMAIN\user";Expression={$_.username}} --> Get w/o variable
$CompInfo `
| Format-Table -Autosize -Property `
@{ Name = "DOMAIN\user"; Expression = { $_.username } },
@{ Name = "Full Name"; Expression = { $fullname } };
Start-Sleep -Seconds 2
}
当第一个脚本获取计算机名时,它将在另一个PowerShell窗口中启动第二个脚本。这意味着您可以让它运行,并使用Ctrl+C退出它,同时在第一次PowerShell会话中仍保持原始脚本运行。为什么需要它在计时器上循环?似乎所有的脚本都可能在用户键入输入时将其从用户下方拉出。如果我正在远程协助用户登录计算机,则希望运行一个循环以刷新以查看用户何时登录计算机。这对他们的输入没有任何影响。这是一个开始,谢谢。但是当我想循环它时,我如何告诉它返回到脚本的顶部呢?所以你并不真的想循环整个脚本,只是在它得到输入后循环一些位?