Hi All,
I have a script that goes to each server checks if the server has rebooted in last 24 hours and if it has it checks which windows updates were installed in the last 24 hours and which automatic services are in stopped state after reboot.
$Machines = Get-Content C:\Scripts\ServersList.csv $LastBootDate = @() $TodayDate = @() $Difference = @() foreach ($Machine in $Machines) { $LastBootDate = Get-WmiObject –Class Win32_OperatingSystem -ComputerName $Machine | Select-Object csname, @{LABEL='LastBootUpTime';Expression={$_.ConverttoDateTime($_.lastbootuptime)}} $TodayDate = Get-Date $Difference = New-TimeSpan –Start $LastBootDate.LastBootUpTime.Date –End $TodayDate.Date if ($Difference.Hours -lt '24' ) { Get-WmiObject -Class "win32_quickfixengineering" -ComputerName $Machine | Where-Object {($_.InstalledOn) -ge (Get-Date).AddDays(-1).Date} | Select-Object -Property "csname","Description", "HotfixID", @{Name="InstalledOn"; Expression={([DateTime]($_.InstalledOn)).ToLocalTime()}} Get-Service -ComputerName $Machine | where { $_.status -ne 'Running' -and $_.Starttype -eq 'Automatic' } | Select-Object MachineName,Name,DisplayName,Status,StartType | ft } else { "$Machine did not reboot yesterday"} }
The above scripts runs well but is very time consuming since it goes to each server one by one and gives the result on the screen. I heard of workflows which allow you to run the same script in parallel. So I have tried the below script which does not give any result
I added the workflow statements and -parallel and -throttlelimit. Finally changed ComputerName to PSComputerName, but not result after running it.
Workflow FindPatchedServers { $Machines = Get-Content C:\Scripts\ServersList.csv $LastBootDate = @() $TodayDate = @() $Difference = @() foreach -Parallel -ThrottleLimit 50 ($Machine in $Machines) { $Workflow:LastBootDate = Get-WmiObject –Class Win32_OperatingSystem -PSComputerName $Machine | Select-Object csname, @{LABEL='LastBootUpTime';Expression={$_.ConverttoDateTime($_.lastbootuptime)}} $Workflow:TodayDate = Get-Date $Workflow:Difference = New-TimeSpan –Start $LastBootDate.LastBootUpTime.Date –End $TodayDate.Date if ($Difference.Hours -lt '24' ) { Get-WmiObject -Class "win32_quickfixengineering" -PSComputerName $Machine | Where-Object {($_.InstalledOn) -ge (Get-Date).AddDays(-1).Date} | Select-Object -Property "csname","Description", "HotfixID", @{Name="InstalledOn"; Expression={([DateTime]($_.InstalledOn)).ToLocalTime()}} Get-Service -PSComputerName $Machine | where { $_.status -ne 'Running' -and $_.Starttype -eq 'Automatic' } | Select-Object MachineName,Name,DisplayName,Status,StartType } else { "$Machine did not reboot yesterday"} } }
Can anyone help me with this please?