Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- Script Disclaimer: The sample scripts provided here are not supported under any Microsoft standard support program or service. All scripts are provided AS IS without warranty of any kind.
- Server Group - Drain Stop Script
- Collects current operation status of all nodes in Cluster - i.e. Paused, Running
- Checks if Localhost is Paused -- $LocalNodeStatus=$True
- Checks if additional nodes in cluster are paused $AdditionalNodeStatus=$True
- If $LocalNodeStatus=$False and $AdditionalNodeStatus=False Drain Stop the local node
- Checks that the local node is in Paused State
- Check that all the VM have evacuated the node
- If the $LocalNodeStatus=$False or $LocalVMNodeStatus=$False implying the drain stop has not been successful report failure
- Issue exit code 1 and revert the node to Running
- Author:
- Twitter @Syswow64blog
- Web: systemcenterblog.co.uk
- #>
- #//Initialize
- $LocalNodeStatus=""
- $AdditionalNodeStatus=""
- #//Setting the log location
- $env:LogPath = "$env:ProgramData\SystemCenterBlog\Logs"
- #// Checking the log path
- if((Test-Path $env:logpath) -eq $False)
- {
- New-Item -path $env:LogPath -type directory
- }
- #Starting the log file
- Start-Transcript -path "$env:LogPath\PatchDrain.log" -append
- #// SMTP Config
- $mail = 'mail.SystemCenterBlog.co.uk'
- $smtp = New-Object Net.Mail.SmtpClient($mail)
- $from = 'SCCMReports@SystemCenterBlog.co.uk'
- $to = 'Admin@SystemCenterBlog.co.uk'
- $sub = "Patch Start Script on [$env:Computername] $Node"
- #// Functions
- Function SendMail($subject,$message,[switch] $fail = $False) {
- Write-Output "$subject`: $message"
- $smtp.Send($from,$to,$subject,$message)
- If ($Fail) {Throw $message}
- }
- #Get Cluster Node Status
- $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
- $ClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State
- #Get local Server Cluster Status-- True if in Paused State
- ForEach($CNode in $ClusterNodes)
- {
- If($CNode.State -eq 2 -and $CNode.name -eq $env:computername)
- {
- #Local Node is in Paused State
- $LocalNodeStatus=$True
- write-host "Local node in Paused state = $LocalNodeStatus -- $CNode .... $(get-date)"
- }
- If($CNode.State -eq 2 -and $CNode.name -NE $env:computername)
- {
- #Another node is in a paused state
- $AdditionalNodeStatus=$True
- write-host "Additional node in Paused State Will not proceed-- $CNode .... $(get-date)"
- }
- }
- If($LocalNodeStatus -NE $True -and $AdditionalNodeStatus -NE $True)
- {
- Write-host "$env:computername Proceed with Drain Script ...................... $(get-date)"
- Suspend-ClusterNode -drain -wait
- #Check that local node has been Paused
- Do
- {
- "$env:computername Checking Node is in a Paused state........................ $(get-date)"
- start-sleep 2
- $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
- } Until ($DownClusterNodes.name -eq $env:computername)
- "$env:computername ...Paused state ........................................... $(get-date)"
- #Check that all VM's have been evacuated
- $x =0
- Do
- {
- "$env:computername Checking Node has evacuated all running VM's.............. $(get-date)"
- start-sleep 5
- #Suspend-ClusterNode -drain -wait
- $x
- $x += 1
- } Until (((get-VM).count -eq 0) -or ($x -eq 3))
- "$env:computername Attempted $x times to move All VM's......................... $(get-date)"
- }
- #//Check Node Status and issue a return code for SCCM
- $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
- If($DownClusterNodes.name -eq $env:computername)
- {
- $LocalNodeStatus=$True
- }
- ELSE
- {
- $LocalNodeStatus=$False
- }
- If((get-VM).count -eq 0)
- {
- $LocalVMNodeStatus=$True
- }
- ELSE
- {
- $LocalVMNodeStatus=$False
- }
- If($LocalNodeStatus -EQ $True -and $LocalVMNodeStatus -EQ $True)
- {
- write-host ""$DownClusterNodes.name" has Operation Status "$DownClusterNodes.state" .... $(get-date)"
- write-host ""$DownClusterNodes.name" $ LocalNodeStatus = $LocalNodeStatus ............ $(get-date)"
- write-host ""$DownClusterNodes.name" $ LocalVMNodeStatus = $LocalVMNodeStatus .......... $(get-date)"
- write-host " SCCM Exit code 0............................................................$(get-date)"
- #SendMail $sub "$env:computername Patch Success End"
- Return 0
- }
- ELSE
- {
- write-host ""$DownClusterNodes.name" has Operation Status "$DownClusterNodes.state" .... $(get-date)"
- write-host "$env:computername $ LocalNodeStatus = $LocalNodeStatus ................... $(get-date)"
- write-host "$env:computername $ LocalVMNodeStatus = $LocalVMNodeStatus ................. $(get-date)"
- Resume-ClusterNode
- write-host "SCCM Exit code 1 ............................................................$(get-date)"
- #SendMail $sub "$env:computername Patch Failed End"
- Return 1
- }
- #SendMail $sub "Patch start script - End"
Add Comment
Please, Sign In to add comment