Morning there
I've been using the below script with great success but want to see if it could be refined. What i intend to do is say rename all vm's with diferent names BUT exclude any vm's where the DNS name contains "local; TMP'
The reason for this is we have a lot of instances where vmware appliances gets deployed where the hostname is localhost and the vApp details is the descriptive part.
Current script:
#Connect-VIServer MyvCenterSRV
# Specify if your virtual machine names include the FQDN
$IncludeFQDN = $false
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
If ($IncludeFQDN) {
$Name = $_.Guest.Hostname
} Else {
$Name = (([string]$_.Guest.HostName).Split("."))[0]
}
If ($_.Name -ne $Name) {
If ($_.Guest.Hostname) {
Write "VM name '$($_.Name)' is not the same as the hostname $Name"
Set-VM $_.Name -Name $Name -whatif -confirm:$false
} Else {
Write "Unable to read hostname for $($_.Name) - No VMTools ?"
}
}
}
Proposed changes:
#Connect-VIServer MyvCenterSRV
# Specify if your virtual machine names include the FQDN
$IncludeFQDN = $false
$excludeNAME = localhost,TEMP,.....(Not sure where to call this)
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
If ($IncludeFQDN) {
$Name = $_.Guest.Hostname
} Else {
$Name = (([string]$_.Guest.HostName).Split("."))[0]
}
If ($_.Name -ne $Name) {
If ($_.Guest.Hostname) {
Write "VM name '$($_.Name)' is not the same as the hostname $Name"
Set-VM $_.Name -Name $Name -whatif -confirm:$false
} Else {
Write "Unable to read hostname for $($_.Name) - No VMTools ?"
}
}
}
Hope someone got a suggestion.
Thanks
Johan
Try something like this
#Connect-VIServer MyvCenterSRV
# Specify if your virtual machine names include the FQDN
$IncludeFQDN = $false
$excludeNAME = "localhost|TEMP"
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
If ($_.Guest.Hostname) {
If ($IncludeFQDN) {
$Name = $_.Guest.Hostname
} Else {
$Name = (([string]$_.Guest.HostName).Split("."))[0]
}
If ($_.Name -ne $Name -and $Name -notmatch $excludeNAME) {
Write "VM name '$($_.Name)' is not the same as the hostname $Name"
Set-VM $_.Name -Name $Name -whatif -confirm:$false
}
}
Else {
Write "Unable to read hostname for $($_.Name) - No VMTools ?"
}
}
The script uses a RegEx expression (match operator) to check if the hostname doesn't contain "localhost" or "TEMP" (the vertical bar in the string does the OR part)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
#Connect-VIServer MyvCenterSRV
# Specify if your virtual machine names include the FQDN
$IncludeFQDN = $false
$excludeNAME = "localhost|TEMP"
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Foreach {
If ($_.Guest.Hostname) {
If ($IncludeFQDN) {
$Name = $_.Guest.Hostname
} Else {
$Name = (([string]$_.Guest.HostName).Split("."))[0]
}
If ($_.Name -ne $Name -and $Name -notmatch $excludeNAME) {
Write "VM name '$($_.Name)' is not the same as the hostname $Name"
Set-VM $_.Name -Name $Name -whatif -confirm:$false
}
}
Else {
Write "Unable to read hostname for $($_.Name) - No VMTools ?"
}
}
The script uses a RegEx expression (match operator) to check if the hostname doesn't contain "localhost" or "TEMP" (the vertical bar in the string does the OR part)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc
You rock, so simple. The scipt is working as expected.
Thanks
Johan