下午好!
我有以下脚本来自动化运行Update-MpSignature,然后使用Start-MpWDOScan运行防御器脱机扫描的过程。我使用 ping 120 运行短期 ping 来验证计算机是否出现故障,但必须等待它,或者中断并重新运行脚本以获得重新打开的提示。有没有办法在收到超时的请求时使用 IF 函数重新启动脚本,以便我可以输入下一台机器?
谢谢
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"
ping $compname -n 120
不要使用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 = "$homedesktopservers.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"状态,这样你就知道你有多深入。