不要使用Ping
,因为它是外部的,使用 PowerShell cmdletTest-Connection
查看服务器是否已关闭,您可以使用Write-Progress
它来跟踪它所处的状态。
param (
[string]$compname = $( Read-Host "Input computer name, please" )
)
Write-Output "Update-MpSignature"
Update-MpSignature -CimSession $compname
Write-Output "Start-MpWDOScan"
Start-MpWDOScan -CimSession $compname
Write-Output "Ping "$compname" for two minutes"
While(Test-Connection $compname -Quiet -Count 1){
Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to shut down."
Start-Sleep -sec 1
}
While(!(Test-Connection $compname -Quiet -Count 1)){
Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to come back up."
Start-Sleep -sec 1
}
编辑:从文件中读取服务器很容易,您只需使用Get-Content
读取文件,然后使用ForEach
循环遍历列表。您需要一个简单的文本文件,每行一个服务器名称。servers.txt
在我的示例中,我将使用位于用户桌面上的名为的文件。您可能希望像这样构造文件:
SQLServerA
FileServerA
WebServerA
SQLServerB
FileServerB
WebServerB
您可以硬编码文件的路径,也可以使用默认位置更改参数以指向文件。然后您可以读取该文件并将内容存储在一个变量中,如下所示:
$complist = Get-Content $listpath
这将设置$complist
为一个字符串数组,其中数组中的每个项目都是文本文件中的一行。接下来,您将使用这样的循环遍历它ForEach
:
ForEach($compname in $complist){
<code to do stuff>
}
所以最后整个事情看起来像这样:
param (
$listpath = "$home\desktop\servers.txt"
)
#Import server list
$complist = Get-Content $listpath
#Loop through the list of servers
ForEach($compname in $complist){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Updating Signature on $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Update-MpSignature -CimSession $compname
Write-Progress -Activity "Processing $compname" -CurrentOperation "Initializing offline scan of $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-MpWDOScan -CimSession $compname
While(Test-Connection $compname -Quiet -Count 1){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to go offline." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-Sleep -sec 1
}
While(!(Test-Connection $compname -Quiet -Count 1)){
Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to come back up." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
Start-Sleep -sec 1
}
}
然后,只要你servers.txt
在桌面上,你就可以运行脚本,它会给你一个状态栏,告诉你它在做什么,它在哪个服务器上工作,并给出“服务器 X of Y”状态,这样你就知道了深入你的东西。