Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Get Credentials
- $Cred = Get-Credential -UserName 'user@domain.tld' -Message 'Please Enter Domain Admin Credentials'
- $ADMachines = Get-ADComputer -Properties * -filter {OperatingSystem -like "*server*"}
- $Online = @()
- $Offline = @()
- if (Test-Path $env:USERPROFILE\Desktop\onlineservers.txt) {Remove-Item $env:USERPROFILE\Desktop\onlineservers.txt}
- if (Test-Path $env:USERPROFILE\Desktop\offlineservers.txt) {Remove-Item $env:USERPROFILE\Desktop\offlineservers.txt}
- if (Test-Path $env:USERPROFILE\Desktop\out.log) {Remove-Item $env:USERPROFILE\Desktop\out.log}
- foreach ($Machine in $ADMachines) {
- if (Test-Connection -count 1 -quiet -computer $Machine.Name) {
- $Online += $Machine.Name }
- else {
- $Offline += $Machine.Name }
- }
- Write-Output "Online: " $Online | Out-File -FilePath $env:USERPROFILE\Desktop\onlineservers.txt -Append
- Write-Output "Offline: " $Offline | Out-File -FilePath $env:USERPROFILE\Desktop\offlineservers.txt -Append
- $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')}
- Write-Host Shutting down servers $Servers -ForegroundColor Yellow -BackgroundColor Red
- foreach ($Server in $Servers) {
- $Session = New-PSSession -ComputerName $Server -Credential $Cred
- Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
- }
- <# how to store a password as secure string
- read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt
- #>
- <# This part doesnt quite work becuase of sudo
- Write-Host "Shutting down Linux Servers." -ForegroundColor Yellow -BackgroundColor Red
- $LinuxServers = @("deb1.domain.tld","deb2.domain.tld")
- $LinuxUser = "user"
- $LinuxPass = cat C:\securestring.txt | convertto-securestring
- $LinuxCred = new-object -typename System.Management.Automation.PSCredential -argumentlist $LinuxUser, $LinuxPass
- Foreach ($LinuxServer in $LinuxServers) {
- $a=New-SSHSession -Computername $LinuxServer -AcceptKey -Credential $LinuxCred
- Invoke-SSHCommand $a.sessionid -Command "sudo halt -p"
- }
- #>
- Write-Host "Shutting down Virtual Domain Controllers." -ForegroundColor Yellow -BackgroundColor Red
- $VirtDCs = @("DC1","DC2","DC3")
- foreach ($VirtDC in $VirtDCs) {
- $Session = New-PSSession -ComputerName $VirtDC -Credential $Cred
- Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
- }
- Write-Host "Shutting down Virtual Hosts." -ForegroundColor Yellow -BackgroundColor Red
- $VMHosts = @("Host1","Host2","Host3")
- foreach ($VMHost in $VMHosts) {
- $Session = New-PSSession -ComputerName $VMHost -Credential $Cred
- Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
- }
- Write-Host "Shutting down physical domain controller." -ForegroundColor Yellow -BackgroundColor Red
- $PhysDCs = @("DC4")
- foreach ($PhysDC in $PhysDCs) {
- $Session = New-PSSession -ComputerName $PhysDC -Credential $Cred
- Invoke-Command -Session $Session -ScriptBlock {Stop-Computer -Force}
- }
- Write-Host "All servers shut down gracefully." -ForegroundColor Yellow -BackgroundColor Red
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement