VMware Cloud Community
Pinball
Enthusiast
Enthusiast
Jump to solution

Set VM = to DNS name

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

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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

Pinball
Enthusiast
Enthusiast
Jump to solution

Luc

You rock, so simple. The scipt is working as expected.

Thanks

Johan

0 Kudos