powershell

sethayes

·

Get-WUReboots

·

Powershell

·

Total Size: 3.63 KB

·

·

Created: 8 months ago

·

Edited: 8 months ago

$hvHosts = @() $nonHosts = @() $hvGuests = @() $physical = @() $all = @() $online = @() $offline = @() $HKLM = [UInt32] "0x80000002"  $User = "msi\msi" $pw = 'p@$$w0rdism' $Pass = $pw | ConvertTo-SecureString -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($User,$Pass) Import-Module ActiveDirectory $all = (Get-ADComputer -LDAPFilter '(&(ObjectCategory=computer)(OperatingSystem=*server*))' | Select Name).Name ForEach ($srv in $all) { If (Test-Connection $srv -Count 1 -Quiet) { $online += $srv } Else { $offline += $srv } } $hvHostsDN = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' ForEach ($hvHostDN in $hvHostsDN) { $hvHosts += $hvHostDN.DistinguishedName.Split(",")[1].Replace("CN=","") } $hvOnline = (Compare-Object $hvHosts $online -IncludeEqual -ExcludeDifferent).InputObject $nonHosts = (Compare-Object $hvOnline $online).InputObject $errors = @() ForEach ($nonHost in $nonHosts) { $type = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $nonHost -Credential $creds -ErrorAction SilentlyContinue) If ($type.Model -like '*Virtual*') { $hvGuests += $nonHost } Else { $physical += $nonHost } } # ForEach ($hvHost in $hvOnline) {$vms = Invoke-Command -ComputerName $hvHost -Credential $cred -ScriptBlock {Get-VM};$result = $hvHost + " | " + $vms.vmname; Write-Output $result} ForEach ($p in $physical) { $WMI_Reg = Get-WmiObject -List StdRegProv -NameSpace root\default -ComputerName $p -Credential $cred -ErrorAction SilentlyContinue $RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\") $WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootPending" If ($WUAURebootReq) { Write-Output "$p requires a reboot for Windows Updates" # Restart-Computer -ComputerName $p -Confirm:$false } Else { Write-Output "$p does not require a reboot" } } ForEach ($g in $hvGuests) { $WMI_Reg = Get-WmiObject -List StdRegProv -NameSpace root\default -ComputerName $g -Credential $cred -ErrorAction SilentlyContinue $RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\") $WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootPending" If ($WUAURebootReq) { Write-Output "$g requires a reboot for Windows Updates" # Restart-Computer -ComputerName $g -Confirm:$false } Else { Write-Output "$g does not require a reboot" } } ForEach ($h in $hvOnline) { $WMI_Reg = Get-WmiObject -List StdRegProv -NameSpace root\default -ComputerName $g -Credential $cred -ErrorAction SilentlyContinue $RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\") $WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootPending" If ($WUAURebootReq) { $vs = Invoke-Command -ComputerName $h -Credential $cred -ScriptBlock {Get-VM} Write-Output "$h requires a reboot for Windows Updates" Write-Output "Virtual Machines Hosted on this Server that will be shutdown`: $($vs.name)" # vs | Stop-VM -Force -Confirm:$False # Start-Sleep 300 Seconds # Invoke-Command -ComputerName $h -Credential $cred -ScriptBlock {Restart-Computer -Force -Confirm:$False} } Else { Write-Output "$h does not require a reboot" } }

2 bits

163 views

Are you sure you want to delete?