######################################
# Prompt to specify vCenter & Cluster
######################################
$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"
##############################
# Connect to specified vCenter
##############################
Connect-VIServer -server $vCenterName -Protocol https -User "username" -Password "Password"
##############################
# Get collection of VMs, create log file
##############################
$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\Dismount-VMware-Tools_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"
##############################
# Get variables for each VM
##############################
foreach ($VM in $colVMs) {
$VMPower = $VM.PowerState
$VMName = $VM.name
$OS = (Get-VMGuest $VM).OSFullName
$vmID = get-vm $VM | foreach {get-view $_.Id}
$vmToolsStatus = $vmID.Guest.ToolsStatus
##############################
# See if VM is powered on
##############################
if ($VMPower -notmatch "PoweredON") {
Write-Host $VMName `t "Powered OFF" -ForegroundColor Red
Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tPowered OFF")
}
##############################
# Update tools on VMs that meet prerequisites
##############################
else {
Dismount-Tools $VM
Write-Host $VMName `t $vmToolStatus `t "Dismounting Tools" -ForegroundColor Green
Add-Content -Path $logFile -Value ($VMName + "`t`tDismounting Tools`t`t" + $vmToolsStatus)
}
}
##############################
# Disconnect from vCenter
##############################
Disconnect-VIServer -server $global:DefaultVIServers -Force
##############################
No comments:
Post a Comment