0

Good afternoon!

I have the following script to automate the process of running Update-MpSignature and then running the defender offline scan with Start-MpWDOScan. I use ping 120 to run a short term ping to verify that the computer goes down but have to either wait it out, or break and re run the script to get the prompt to open back up. Is there a way to use a IF function upon receiving a request timed out to restart the script so i can input the next machine?

Thanks

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
4

1 回答 1

0

不要使用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”状态,这样你就知道了深入你的东西。

于 2018-03-09T19:59:41.813 回答