Last active
September 9, 2017 14:21
-
-
Save mrlesmithjr/7f44834952a8e09209a3cb8d47d904d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| <Overview of script> | |
| .DESCRIPTION | |
| <Brief description of script> | |
| .PARAMETER <Parameter_Name> | |
| <Brief description of parameter input required. Repeat this attribute if required> | |
| .INPUTS | |
| <Inputs if any, otherwise state None> | |
| .OUTPUTS Log File | |
| The script log file stored in C:\Windows\Temp\<name>.log | |
| .NOTES | |
| Version: 1.0 | |
| Author: <Name> | |
| Creation Date: <Date> | |
| Purpose/Change: Initial script development | |
| .EXAMPLE | |
| <Example explanation goes here> | |
| <Example goes here. Repeat this attribute for more than one example> | |
| #> | |
| $TimeStamp = ($(Get-Date -f MM-dd-yyyy)+ "_" + $(get-date -f HH_mm_ss)) | |
| $Log= 'c:\tmp\powercli_script_' + $TimeStamp + ".log" | |
| Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null | |
| {% for host in groups['vsphere_hosts'] %} | |
| $vi_server="{{ hostvars[host]['ansible_host'] }}" | |
| $vc_user="{{ vsphere_user_info['username'] }}" | |
| $vc_pass="{{ vsphere_user_info['password'] }}" | |
| # Connect to vSphere Host/vCenter | |
| Connect-VIServer -Server $vi_server -User $vc_user -Password $vc_pass | |
| {% if hostvars[host]['vsphere_enable_ssh'] is defined %} | |
| # Managing SSH Service | |
| $CurrentSSHStatus=$(Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Select-Object -ExpandProperty Running) | |
| {% if hostvars[host]['vsphere_enable_ssh'] %} | |
| if ($CurrentSSHStatus -ne $true) { | |
| "Starting SSH on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Start-VMHostService -Confirm:$false | Out-File $Log -Append | |
| } | |
| {% elif not hostvars[host]['vsphere_enable_ssh'] %} | |
| if ($CurrentSSHStatus -ne $false) { | |
| "Stopping SSH on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false | Out-File $Log -Append | |
| } | |
| {% endif %} | |
| {% endif %} | |
| {% if hostvars[host]['vsphere_ntp_servers'] is defined %} | |
| # Managing NTP Service | |
| $CurrentNtpServers=@($(Get-VMHostNtpServer)) | |
| $NewNtpServers=@($("{{ hostvars[host]['vsphere_ntp_servers']|join('", "') }}")) | |
| Foreach ($i in $NewNtpServers){ | |
| if ($CurrentNtpServers -notcontains $i) { | |
| "Adding NTP server $i on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Add-VmHostNtpServer -NtpServer $i -Confirm:$false | Out-File $Log -Append | |
| $RestartNtp="true" | |
| } | |
| } | |
| Foreach ($i in $CurrentNtpServers) { | |
| if ($NewNtpServers -notcontains $i) { | |
| "Removing NTP server $i on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Remove-VmHostNtpServer -NtpServer $i -Confirm:$false | Out-File $Log -Append | |
| $RestartNtp="true" | |
| } | |
| } | |
| if ($RestartNtp) { | |
| if ($RestartNtp -eq "true") { | |
| "Restarting NTP service on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Get-VMHostService | Where-Object { $_.Key -eq 'ntpd' } | Restart-VMHostService -Confirm:$false | Out-File $Log -Append | |
| } | |
| } | |
| {% endif %} | |
| {% if hostvars[host]['vsphere_remove_default_vm_network'] is defined and hostvars[host]['vsphere_remove_default_vm_network'] %} | |
| # Delete Default VM Network | |
| $PortGroups=@($(Get-VirtualPortGroup | Select-Object -ExpandProperty Name)) | |
| if ($PortGroups -contains 'VM Network') { | |
| $VMNetwork=$(Get-VirtualPortGroup -Name 'VM Network') | |
| Remove-VirtualPortGroup -VirtualPortGroup $VMNetwork -Confirm:$false | Out-File $Log -Append | |
| } | |
| {% endif %} | |
| {% if hostvars[host]['vsphere_vswitches'] is defined %} | |
| # Managing VSS vSwitch Interfaces | |
| {% for vswitch in hostvars[host]['vsphere_vswitches'] %} | |
| $CurrentNics=@($(Get-VirtualSwitch -Name {{ vswitch['name'] }}| Get-VMHostNetworkAdapter -Physical | Select-Object -ExpandProperty Name)) | |
| $NewNics=@($("{{ vswitch['nics']|join('", "') }}")) | |
| Foreach ($i in $NewNics){ | |
| if ($CurrentNics -notcontains $i){ | |
| $NewNic=$(Get-VMHostNetworkAdapter -Physical -Name $i) | |
| Get-VirtualSwitch -Name {{ vswitch['name'] }} | Add-VirtualSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $NewNic -Confirm:$false | Out-File $Log -Append | |
| } | |
| } | |
| $ActiveNics=$(Get-VirtualSwitch -Name {{ vswitch['name'] }} | Get-NicTeamingPolicy | Select-Object -ExpandProperty ActiveNic) | |
| Foreach ($i in $NewNics) { | |
| if ($ActiveNics -notcontains $i){ | |
| Get-VirtualSwitch -Name {{ vswitch['name'] }} | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $i | Out-File $Log -Append | |
| } | |
| } | |
| {% endfor %} | |
| {% endif %} | |
| {% if hostvars[host]['vsphere_datastores'] is defined %} | |
| # Managing Datastores | |
| Get-DataStore -refresh | |
| $CurrentNFSDataStores=@($(Get-Datastore | Where {$_.type -eq "NFS"} | Select-Object -ExpandProperty Name)) | |
| {% for datastore in hostvars[host]['vsphere_datastores'] %} | |
| {% if datastore['type']|lower == 'nfs' %} | |
| if ($CurrentNFSDataStores -notcontains '{{ datastore['name'] }}'){ | |
| New-Datastore -Nfs -Name '{{ datastore['name'] }}' -Path '{{ datastore['path'] }}' -NfsHost {{ datastore['host'] }} | Out-File $Log -Append | |
| {% endif %} | |
| } | |
| {% endfor %} | |
| {% endif %} | |
| {% if hostvars[host]['vsphere_maintenance_mode'] is defined %} | |
| # Managing Maintenance Mode | |
| $MaintenanceMode=$(Get-VMHost | Select-Object -ExpandProperty State) | |
| {% if hostvars[host]['vsphere_maintenance_mode'] %} | |
| if ($MaintenanceMode -ne 'Maintenance') { | |
| "Entering Maintenance mode on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Set-VMHost -State 'Maintenance' -Confirm:$false | Out-File $Log -Append | |
| } | |
| {% if hostvars[host]['vsphere_hosts_update'] is defined and hostvars[host]['vsphere_hosts_update'] %} | |
| #(Get-ESXCli).software.vib.list() | |
| # Managing Updates | |
| $SystemVersion=$((Get-EsxCli).system.version.get().version) | |
| $SystemVersionUpdate=$((Get-EsxCli).system.version.get().update) | |
| $SystemVersionBuild=$((Get-EsxCli).system.version.get().build.Trim("Releasebuild-")) | |
| $SystemAvailableUpdateBuilds=@($("{% for _build in hostvars[host]['vsphere_updates'] %}{{ _build['build'] }}{% if not loop.last %}", "{% endif %}{% endfor %}")) | |
| $SystemAvailableUpdateBuilds=$SystemAvailableUpdateBuilds|sort | |
| Foreach ($i in $SystemAvailableUpdateBuilds) { | |
| if ($SystemVersionBuild -lt $i) { | |
| $SystemUpdateBuild="$i" | |
| } | |
| } | |
| {% if hostvars[host]['vsphere_updates'] is defined %} | |
| {% for update in hostvars[host]['vsphere_updates'] %} | |
| if ($SystemVersion -eq '{{ update['version'] }}') { | |
| if ($SystemUpdateBuild -eq '{{ update['build'] }}') { | |
| "Updating host {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Install-VMHostPatch -HostPath '{{ update['path'] }}/metadata.zip' -Confirm:$false | Out-File $Log -Append | |
| $RebootHost="true" | |
| } | |
| } | |
| {% endfor %} | |
| {% endif %} | |
| {% endif %} | |
| {% elif not hostvars[host]['vsphere_maintenance_mode'] %} | |
| if ($MaintenanceMode -ne 'Connected') { | |
| "Exiting Maintenance mode on {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Set-VMHost -State 'Connected' -Confirm:$false | Out-File $Log -Append | |
| } | |
| {% endif %} | |
| {% endif %} | |
| if ($RebootHost) { | |
| if ($RebootHost -eq "true") { | |
| "Rebooting {{ hostvars[host]['inventory_hostname'] }}" | Out-File $Log -Append | |
| Restart-VMHost -Confirm:$false | Out-File $Log -Append | |
| # Wait 30 Seconds For Server To Stop Responding | |
| if (Test-Connection -Cn {{ hostvars[host]['ansible_host'] }} -BufferSize 16 -Count 1 -ea 0 -quiet) { | |
| "Server {{ hostvars[host]['ansible_host'] }} still responding after 30 seconds" | Out-File $Log -Append | |
| sleep 30 | |
| } | |
| # Wait 60 Seconds For Server To Stop Responding | |
| if (Test-Connection -Cn {{ hostvars[host]['ansible_host'] }} -BufferSize 16 -Count 1 -ea 0 -quiet) { | |
| "Server {{ hostvars[host]['ansible_host'] }} still responding after 60 seconds" | Out-File $Log -Append | |
| sleep 60 | |
| } | |
| } | |
| } | |
| Disconnect-VIServer * -Confirm:$false | |
| {% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment