Advertisement
bblades262

Emergency Shutdown

Jul 13th, 2016
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Get Credentials
  2. $Cred = Get-Credential -UserName 'user@domain.tld' -Message 'Please Enter Domain Admin Credentials'
  3. $ADMachines = Get-ADComputer -Properties * -filter {OperatingSystem -like "*server*"}
  4. $Online = @()
  5. $Offline = @()
  6. if (Test-Path $env:USERPROFILE\Desktop\onlineservers.txt) {Remove-Item $env:USERPROFILE\Desktop\onlineservers.txt}
  7. if (Test-Path $env:USERPROFILE\Desktop\offlineservers.txt) {Remove-Item $env:USERPROFILE\Desktop\offlineservers.txt}
  8. if (Test-Path $env:USERPROFILE\Desktop\out.log) {Remove-Item $env:USERPROFILE\Desktop\out.log}
  9. foreach ($Machine in $ADMachines) {
  10.     if (Test-Connection -count 1 -quiet -computer $Machine.Name) {
  11.     $Online += $Machine.Name }
  12.     else {
  13.     $Offline += $Machine.Name }
  14. }
  15. Write-Output "Online: " $Online | Out-File -FilePath $env:USERPROFILE\Desktop\onlineservers.txt -Append
  16. Write-Output "Offline: " $Offline | Out-File -FilePath $env:USERPROFILE\Desktop\offlineservers.txt -Append
  17.  
  18. $Servers = $Online | Where-Object { ($_ -notmatch 'DC1') -and ($_ -notmatch 'DC2') -and ($_ -notmatch 'DC3') -and ($_ -notmatch 'DC4') -and ($_ -notmatch 'Host1') -and ($_ -notmatch 'Host2') -and ($_ -notmatch 'Host3') -and ($_ -notmatch 'Cluster1')}
  19.  
  20. Write-Host Shutting down servers $Servers -ForegroundColor Yellow -BackgroundColor Red
  21.  
  22. foreach ($Server in $Servers) {
  23.   $Session = New-PSSession -ComputerName $Server -Credential $Cred
  24.   Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
  25. }
  26.  
  27. <# how to store a password as secure string
  28. read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt
  29. #>
  30.  
  31. <# This part doesnt quite work becuase of sudo
  32. Write-Host "Shutting down Linux Servers." -ForegroundColor Yellow -BackgroundColor Red
  33. $LinuxServers = @("deb1.domain.tld","deb2.domain.tld")
  34. $LinuxUser = "user"
  35. $LinuxPass = cat C:\securestring.txt | convertto-securestring
  36. $LinuxCred = new-object -typename System.Management.Automation.PSCredential -argumentlist $LinuxUser, $LinuxPass
  37. Foreach ($LinuxServer in $LinuxServers) {
  38.   $a=New-SSHSession -Computername $LinuxServer -AcceptKey -Credential $LinuxCred
  39.   Invoke-SSHCommand $a.sessionid -Command "sudo halt -p"
  40. }
  41. #>
  42.  
  43. Write-Host "Shutting down Virtual Domain Controllers." -ForegroundColor Yellow -BackgroundColor Red
  44.  
  45. $VirtDCs = @("DC1","DC2","DC3")
  46. foreach ($VirtDC in $VirtDCs) {
  47.   $Session = New-PSSession -ComputerName $VirtDC -Credential $Cred
  48.   Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
  49. }
  50.  
  51.  
  52. Write-Host "Shutting down Virtual Hosts."  -ForegroundColor Yellow -BackgroundColor Red
  53.  
  54. $VMHosts = @("Host1","Host2","Host3")
  55. foreach ($VMHost in $VMHosts) {
  56.   $Session = New-PSSession -ComputerName $VMHost -Credential $Cred
  57.   Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
  58. }
  59.  
  60.  
  61. Write-Host "Shutting down physical domain controller." -ForegroundColor Yellow -BackgroundColor Red
  62.  
  63. $PhysDCs = @("DC4")
  64. foreach ($PhysDC in $PhysDCs) {
  65.   $Session = New-PSSession -ComputerName $PhysDC -Credential $Cred
  66.   Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
  67. }
  68.  
  69.  
  70. Write-Host  "All servers shut down gracefully." -ForegroundColor Yellow -BackgroundColor Red
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement