Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. <Overview of script>
  4. .DESCRIPTION
  5. <Brief description of script>
  6. .PARAMETER <Parameter_Name>
  7. <Brief description of parameter input required. Repeat this attribute if required>
  8. .INPUTS
  9. <Inputs if any, otherwise state None>
  10. .OUTPUTS Log File
  11. The script log file stored in C:\Windows\Temp\<name>.log
  12. .NOTES
  13. Version: 1.0
  14. Author: <Name>
  15. Creation Date: <Date>
  16. Purpose/Change: Initial script development
  17. .EXAMPLE
  18. <Example explanation goes here>
  19.  
  20. <Example goes here. Repeat this attribute for more than one example>
  21. #>
  22.  
  23. Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
  24. {% for host in groups['vsphere_hosts'] %}
  25. $vi_server="{{ hostvars[host]['ansible_host'] }}"
  26. $vc_user="{{ vsphere_user_info['username'] }}"
  27. $vc_pass="{{ vsphere_user_info['password'] }}"
  28.  
  29. # Connect to vSphere Host/vCenter
  30. Connect-VIServer -Server $vi_server -User $vc_user -Password $vc_pass
  31.  
  32. {% if hostvars[host]['vsphere_enable_ssh'] is defined %}
  33. # Managing SSH Service
  34. $CurrentSSHStatus=$(Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Select-Object -ExpandProperty Running)
  35. {% if hostvars[host]['vsphere_enable_ssh'] %}
  36. if ($CurrentSSHStatus -ne $true) {
  37. Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Start-VMHostService -Confirm:$false
  38. }
  39. {% elif not hostvars[host]['vsphere_enable_ssh'] %}
  40. if ($CurrentSSHStatus -ne $false) {
  41. Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false
  42. }
  43. {% endif %}
  44. {% endif %}
  45.  
  46. {% if hostvars[host]['vsphere_ntp_servers'] is defined %}
  47. # Managing NTP Service
  48. $CurrentNtpServers=@($(Get-VMHostNtpServer))
  49. $NewNtpServers=@($("{{ hostvars[host]['vsphere_ntp_servers']|join('", "') }}"))
  50. Foreach ($i in $NewNtpServers){
  51. if ($CurrentNtpServers -notcontains $i) {
  52. Add-VmHostNtpServer -NtpServer $i -Confirm:$false
  53. $RestartNtp="true"
  54. }
  55. }
  56. Foreach ($i in $CurrentNtpServers) {
  57. if ($NewNtpServers -notcontains $i) {
  58. Remove-VmHostNtpServer -NtpServer $i -Confirm:$false
  59. $RestartNtp="true"
  60. }
  61. }
  62. if ($RestartNtp) {
  63. if ($RestartNtp -eq "true") {
  64. Get-VMHostService | Where-Object { $_.Key -eq 'ntpd' } | Restart-VMHostService -Confirm:$false
  65. }
  66. }
  67. {% endif %}
  68.  
  69. {% if hostvars[host]['vsphere_remove_default_vm_network'] is defined and hostvars[host]['vsphere_remove_default_vm_network'] %}
  70. # Delete Default VM Network
  71. $PortGroups=@($(Get-VirtualPortGroup | Select-Object -ExpandProperty Name))
  72. if ($PortGroups -contains 'VM Network') {
  73. $VMNetwork=$(Get-VirtualPortGroup -Name 'VM Network')
  74. Remove-VirtualPortGroup -VirtualPortGroup $VMNetwork -Confirm:$false
  75. }
  76. {% endif %}
  77.  
  78. {% if hostvars[host]['vsphere_vswitches'] is defined %}
  79. # Managing VSS vSwitch Interfaces
  80. {% for vswitch in hostvars[host]['vsphere_vswitches'] %}
  81. $CurrentNics=@($(Get-VirtualSwitch -Name {{ vswitch['name'] }}| Get-VMHostNetworkAdapter -Physical | Select-Object -ExpandProperty Name))
  82. $NewNics=@($("{{ vswitch['nics']|join('", "') }}"))
  83. Foreach ($i in $NewNics){
  84. if ($CurrentNics -notcontains $i){
  85. $NewNic=$(Get-VMHostNetworkAdapter -Physical -Name $i)
  86. Get-VirtualSwitch -Name {{ vswitch['name'] }} | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $NewNic -Confirm:$false
  87. }
  88. }
  89. $ActiveNics=$(Get-VirtualSwitch -Name {{ vswitch['name'] }} | Get-NicTeamingPolicy | Select-Object -ExpandProperty ActiveNic)
  90. Foreach ($i in $NewNics) {
  91. if ($ActiveNics -notcontains $i){
  92. Get-VirtualSwitch -Name {{ vswitch['name'] }} | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $i
  93. }
  94. }
  95. {% endfor %}
  96. {% endif %}
  97.  
  98. {% if hostvars[host]['vsphere_datastores'] is defined %}
  99. # Managing Datastores
  100. Get-DataStore -refresh
  101. $CurrentNFSDataStores=@($(Get-Datastore | Where {$_.type -eq "NFS"} | Select-Object -ExpandProperty Name))
  102. {% for datastore in hostvars[host]['vsphere_datastores'] %}
  103. {% if datastore['type']|lower == 'nfs' %}
  104. if ($CurrentNFSDataStores -notcontains '{{ datastore['name'] }}'){
  105. New-Datastore -Nfs -Name '{{ datastore['name'] }}' -Path '{{ datastore['path'] }}' -NfsHost {{ datastore['host'] }}
  106. {% endif %}
  107. }
  108. {% endfor %}
  109. {% endif %}
  110.  
  111. {% if hostvars[host]['vsphere_maintenance_mode'] is defined %}
  112. # Managing Maintenance Mode
  113. $MaintenanceMode=$(Get-VMHost | Select-Object -ExpandProperty State)
  114. {% if hostvars[host]['vsphere_maintenance_mode'] %}
  115. if ($MaintenanceMode -ne 'Maintenance') {
  116. Set-VMHost -State 'Maintenance' -Evacuate -Confirm:$false
  117. }
  118. {% if hostvars[host]['vsphere_hosts_update'] is defined and hostvars[host]['vsphere_hosts_update'] %}
  119. #(Get-ESXCli).software.vib.list()
  120. # Managing Updates
  121. $SystemVersion=$((Get-EsxCli).system.version.get().version)
  122. $SystemVersionUpdate=$((Get-EsxCli).system.version.get().update)
  123. $SystemVersionBuild=$((Get-EsxCli).system.version.get().build.Trim("Releasebuild-"))
  124. $SystemAvailableUpdateBuilds=@($("{% for _build in hostvars[host]['vsphere_updates'] %}{{ _build['build'] }}{% if not loop.last %}", "{% endif %}{% endfor %}"))
  125. $SystemAvailableUpdateBuilds=$SystemAvailableUpdateBuilds|sort
  126. Foreach ($i in $SystemAvailableUpdateBuilds) {
  127. if ($SystemVersionBuild -lt $i) {
  128. $SystemUpdateBuild="$i"
  129. }
  130. }
  131. {% if hostvars[host]['vsphere_updates'] is defined %}
  132. {% for update in hostvars[host]['vsphere_updates'] %}
  133. if ($SystemVersion -eq '{{ update['version'] }}') {
  134. if ($SystemUpdateBuild -eq '{{ update['build'] }}') {
  135. Install-VMHostPatch -HostPath '{{ update['path'] }}/metadata.zip' -Confirm:$false
  136. $RebootHost="true"
  137. }
  138. }
  139. {% endfor %}
  140. {% endif %}
  141. {% endif %}
  142. {% elif not hostvars[host]['vsphere_maintenance_mode'] %}
  143. if ($MaintenanceMode -ne 'Connected') {
  144. Set-VMHost -State 'Connected' -Confirm:$false
  145. }
  146. {% endif %}
  147. {% endif %}
  148.  
  149. if ($RebootHost) {
  150. if ($RebootHost -eq "true") {
  151. Restart-VMHost -Confirm:$false
  152. # Wait For Server To Stop Responding
  153. do {
  154. } Until (!(Test-Connection {{ hostvars[host]['ansible_host'] }} -Quiet -Count 1))
  155. # Wait For Server To Start Responding
  156. do {
  157. } Until ((Test-Connection {{ hostvars[host]['ansible_host'] }} -Quiet -Count 1))
  158. }
  159. }
  160. Disconnect-VIServer * -Confirm:$false
  161. {% endfor %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement