Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KurtDeGreeff/48d8412a9c02a4355bfded276b4b1518 to your computer and use it in GitHub Desktop.
Save KurtDeGreeff/48d8412a9c02a4355bfded276b4b1518 to your computer and use it in GitHub Desktop.
<#
.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>
#>
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) {
Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Start-VMHostService -Confirm:$false
}
{% elif not hostvars[host]['vsphere_enable_ssh'] %}
if ($CurrentSSHStatus -ne $false) {
Get-VMHostService | Where-Object {$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false
}
{% 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) {
Add-VmHostNtpServer -NtpServer $i -Confirm:$false
$RestartNtp="true"
}
}
Foreach ($i in $CurrentNtpServers) {
if ($NewNtpServers -notcontains $i) {
Remove-VmHostNtpServer -NtpServer $i -Confirm:$false
$RestartNtp="true"
}
}
if ($RestartNtp) {
if ($RestartNtp -eq "true") {
Get-VMHostService | Where-Object { $_.Key -eq 'ntpd' } | Restart-VMHostService -Confirm:$false
}
}
{% 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
}
{% 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
}
}
$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
}
}
{% 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'] }}
{% 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') {
Set-VMHost -State 'Maintenance' -Evacuate -Confirm:$false
}
{% 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'] }}') {
Install-VMHostPatch -HostPath '{{ update['path'] }}/metadata.zip' -Confirm:$false
$RebootHost="true"
}
}
{% endfor %}
{% endif %}
{% endif %}
{% elif not hostvars[host]['vsphere_maintenance_mode'] %}
if ($MaintenanceMode -ne 'Connected') {
Set-VMHost -State 'Connected' -Confirm:$false
}
{% endif %}
{% endif %}
if ($RebootHost) {
if ($RebootHost -eq "true") {
Restart-VMHost -Confirm:$false
# Wait For Server To Stop Responding
do {
} Until (!(Test-Connection {{ hostvars[host]['ansible_host'] }} -Quiet -Count 1))
# Wait For Server To Start Responding
do {
} Until ((Test-Connection {{ hostvars[host]['ansible_host'] }} -Quiet -Count 1))
}
}
Disconnect-VIServer * -Confirm:$false
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment