Guest User

Untitled

a guest
Jun 19th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <#
  2. 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.
  3.  
  4. Server Group - Drain Stop Script
  5. Collects current operation status of all nodes in Cluster - i.e. Paused, Running
  6. Checks if Localhost is Paused -- $LocalNodeStatus=$True
  7. Checks if additional nodes in cluster are paused $AdditionalNodeStatus=$True
  8.  
  9. If $LocalNodeStatus=$False and $AdditionalNodeStatus=False Drain Stop the local node
  10. Checks that the local node is in Paused State
  11. Check that all the VM have evacuated the node
  12.  
  13. If the $LocalNodeStatus=$False or $LocalVMNodeStatus=$False implying the drain stop has not been successful report failure
  14. Issue exit code 1 and revert the node to Running
  15.  
  16. Author:
  17. Twitter @Syswow64blog
  18. Web: systemcenterblog.co.uk
  19.  
  20. #>
  21.  
  22.  
  23. #//Initialize
  24. $LocalNodeStatus=""
  25. $AdditionalNodeStatus=""
  26.  
  27. #//Setting the log location
  28. $env:LogPath = "$env:ProgramData\SystemCenterBlog\Logs"
  29.  
  30. #// Checking the log path
  31. if((Test-Path $env:logpath) -eq $False)
  32. {
  33. New-Item -path $env:LogPath -type directory
  34. }
  35.  
  36. #Starting the log file
  37. Start-Transcript -path "$env:LogPath\PatchDrain.log" -append
  38.  
  39.  
  40. #// SMTP Config
  41. $mail = 'mail.SystemCenterBlog.co.uk'
  42. $smtp = New-Object Net.Mail.SmtpClient($mail)
  43. $from = 'SCCMReports@SystemCenterBlog.co.uk'
  44. $to = 'Admin@SystemCenterBlog.co.uk'
  45. $sub = "Patch Start Script on [$env:Computername] $Node"
  46.  
  47. #// Functions
  48. Function SendMail($subject,$message,[switch] $fail = $False) {
  49. Write-Output "$subject`: $message"
  50. $smtp.Send($from,$to,$subject,$message)
  51. If ($Fail) {Throw $message}
  52. }
  53.  
  54. #Get Cluster Node Status
  55. $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
  56. $ClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State
  57.  
  58. #Get local Server Cluster Status-- True if in Paused State
  59. ForEach($CNode in $ClusterNodes)
  60. {
  61. If($CNode.State -eq 2 -and $CNode.name -eq $env:computername)
  62. {
  63. #Local Node is in Paused State
  64. $LocalNodeStatus=$True
  65. write-host "Local node in Paused state = $LocalNodeStatus -- $CNode .... $(get-date)"
  66. }
  67.  
  68. If($CNode.State -eq 2 -and $CNode.name -NE $env:computername)
  69. {
  70. #Another node is in a paused state
  71. $AdditionalNodeStatus=$True
  72. write-host "Additional node in Paused State Will not proceed-- $CNode .... $(get-date)"
  73. }
  74.  
  75. }
  76.  
  77. If($LocalNodeStatus -NE $True -and $AdditionalNodeStatus -NE $True)
  78. {
  79. Write-host "$env:computername Proceed with Drain Script ...................... $(get-date)"
  80. Suspend-ClusterNode -drain -wait
  81.  
  82. #Check that local node has been Paused
  83. Do
  84. {
  85.  
  86. "$env:computername Checking Node is in a Paused state........................ $(get-date)"
  87.  
  88. start-sleep 2
  89. $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
  90.  
  91.  
  92. } Until ($DownClusterNodes.name -eq $env:computername)
  93. "$env:computername ...Paused state ........................................... $(get-date)"
  94.  
  95. #Check that all VM's have been evacuated
  96. $x =0
  97. Do
  98. {
  99.  
  100. "$env:computername Checking Node has evacuated all running VM's.............. $(get-date)"
  101.  
  102. start-sleep 5
  103. #Suspend-ClusterNode -drain -wait
  104. $x
  105. $x += 1
  106.  
  107. } Until (((get-VM).count -eq 0) -or ($x -eq 3))
  108. "$env:computername Attempted $x times to move All VM's......................... $(get-date)"
  109.  
  110. }
  111.  
  112.  
  113.  
  114. #//Check Node Status and issue a return code for SCCM
  115.  
  116. $DownClusterNodes = Get-WmiObject -Class "MSCluster_Node" -Namespace "root\MSCluster" | Select-Object name, State | Where-Object {$_.state -ne 0}
  117. If($DownClusterNodes.name -eq $env:computername)
  118. {
  119. $LocalNodeStatus=$True
  120. }
  121. ELSE
  122. {
  123. $LocalNodeStatus=$False
  124. }
  125.  
  126.  
  127. If((get-VM).count -eq 0)
  128. {
  129. $LocalVMNodeStatus=$True
  130. }
  131. ELSE
  132. {
  133. $LocalVMNodeStatus=$False
  134. }
  135.  
  136. If($LocalNodeStatus -EQ $True -and $LocalVMNodeStatus -EQ $True)
  137. {
  138. write-host ""$DownClusterNodes.name" has Operation Status "$DownClusterNodes.state" .... $(get-date)"
  139. write-host ""$DownClusterNodes.name" $ LocalNodeStatus = $LocalNodeStatus ............ $(get-date)"
  140. write-host ""$DownClusterNodes.name" $ LocalVMNodeStatus = $LocalVMNodeStatus .......... $(get-date)"
  141. write-host " SCCM Exit code 0............................................................$(get-date)"
  142. #SendMail $sub "$env:computername Patch Success End"
  143. Return 0
  144. }
  145. ELSE
  146. {
  147. write-host ""$DownClusterNodes.name" has Operation Status "$DownClusterNodes.state" .... $(get-date)"
  148. write-host "$env:computername $ LocalNodeStatus = $LocalNodeStatus ................... $(get-date)"
  149. write-host "$env:computername $ LocalVMNodeStatus = $LocalVMNodeStatus ................. $(get-date)"
  150. Resume-ClusterNode
  151. write-host "SCCM Exit code 1 ............................................................$(get-date)"
  152. #SendMail $sub "$env:computername Patch Failed End"
  153. Return 1
  154. }
  155.  
  156. #SendMail $sub "Patch start script - End"
Add Comment
Please, Sign In to add comment