PowerShell or WMI to get Guest Name

This topic contains 5 replies, has 2 voices, and was last updated by  Chris 3 months ago.

  • Author
    Posts
  • #71359

    Chris
    Participant

    In PowerShell using Get-VM I found properties for VMName, ComputerName and Name but they all refer to the host. When I deploy its a sysprepped image with a random WIN-name. I'm considering placing a simple rename-computer script on the images and call them. Is there an easier way with wmi or PowerShell?

  • #71369

    Sam Boutros
    Participant

    Depends on what version of hyper-V you're using.
    If you're using Hyper-V of Server 2016, you have access to PowerShell Direct where you can open a remote PowerShell session to the VM from the host – see this link for example. Again this is ONLY available in Server 2016 Hyper-V and NOT in older versions like Server 2012 R2 Hyper-V.

    If you're using Server 2012 R2 as your hypervisor, I recommend you use an unattended.xml file with your generalized VM image to setup things like VMName, and local administrator password. You can then use PowerShell Remoting to the VM from the parent partition (Hyper-V host) over the 169.254.0.0/16 IP (I will be posting a function to automate this process in the next few days or weeks). You can then do initial VM config like IP addressing, AD setup, time zone, roles and features setup, firewall config, ...

    Other more complicated techniques include downing the VM, attaching its VHDX file to another VM or the hyper-v host, and injecting such information in the offline VM VHDX file. Unless you're using shielded VMs/guarded fabric..

  • #71372

    Chris
    Participant

    I have a windows 10 desktop and can load module 1.1 or 2.0.0.0. I do have both server 2012 r2 and server 2016 to manage.
    ESXi 6 lets me rename a vm guest prior to adding it to the domain like this. I heard you could use wmi to get to the guest name but cant find any examples.

    $VCName = $VM.Name
    				$WinName = $VM.Guest.Hostname
    				If ($WinName -ne $VCName)
    				{
    					$progressbar1.PerformStep()
    					Write-Host "$VCName is currently $WinName... renaming"
    					$renamecomputer = "wmic path win32_computersystem where ""Name='%computername%'"" CALL rename name='$VCName'"
    					Invoke-VMScript -VM $VM -GuestUser "Administrator" -GuestPassword "somegr8pwhere" -ScriptType Bat -ScriptText $renamecomputer
    					restart-vmguest -VM $VM -Confirm:$false
    					$progressbar1.PerformStep()
  • #71378

    Chris
    Participant

    So I came up with a silly way to solve this without having to go submit tickets to another department and waiting for weeks. At startup I'll place a script to push a file of the machine name to the host. I can get content on the file sitting on host. Now I have a vm machine name!

    $x = hostname
    if ($x -eq "WIN-*"){
        $x | Out-File V:\Log\vmname.txt -force
    }
    
    Else
    
    {
        Return
    }
  • #71380

    Chris
    Participant

    Another way I just realised is when the machine registers on the network either static IP or if it gets DHCP you can get the name like this.

    Get-VMNetworkAdapter -VMName Web1
    [System.Net.Dns]::GetHostbyAddress(“192.168.100.29”)
  • #71383

    Chris
    Participant

    So here's a ugly method that will do the same thing as the VMWare script rename computer. You must have an IPAddress either statically assigned when you provision or one from DHCP when you connect the vnic. It works on both server 2012 R2 and 2016. I've seen some other methods to do this but none worked for me.

    $x = Get-VMNetworkAdapter -VMName Web1 | Select -ExpandProperty IPAddresses
    $y = ($x -Split'\n')[0]
    $z = [System.Net.Dns]::GetHostbyAddress(“$y”)|Select -ExpandProperty Hostname
    New-PSSession -ComputerName $z -Credential administrator
    $a = Get-PSSession |where ComputerName -EQ $z| select -ExpandProperty Id
    Enter-PSSession -Id $a|Rename-Computer -NewName Web1 
    Exit-PSSession
    Remove-PSSession -Id $a
    Restart-VM -Name Web1 -Force -Confirm:$false

You must be logged in to reply to this topic.