VMware Cloud Community
DTsaliabine
Contributor
Contributor

Get Current VM name

Hi,

I've just started with PowerCLI and have following trouble - I need to obtain VM name from wich I run my script (I have to clone a lot of MS servers and want to completly automate it, some of parametrs are based on VM name).

Is it possible over PowerCLI?

Tags (4)
0 Kudos
6 Replies
mattboren
Expert
Expert

Hello, DTsaliabine-

To get the host name of the machine on which your PowerShell script is running, you can use the environment variable, "Computername", like:

PS C:\> $env:Computername

vm01

You could also use hostname.exe, which is standard on Windows.

0 Kudos
Zsoldier
Expert
Expert

To add onto Matt, you can find out what environment variables are available to you by using the get-childitem cmdlet.

Get-ChildItem env:

This will get you a list.  To call them, you can reference them like Matt demo'd above.

$env:variablename

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
DTsaliabine
Contributor
Contributor

Hi,

Thanks, for your answers!

I want to setup fully automate MS lab creation, so I've prepared templates (syspred with some addition scripts on startup). After sysprep computer name is randomly generated (like MYVM-fjkln3058), I want automaticly (using sysprep startup script) change computer name so it correspond VM name in VMWare. (for example in Vcenter VM name="Win2k3-232" and change computer name to "232" and set it IP to "10.1.2.232"). Thats why I need to obtain VM name from vcenter.

0 Kudos
Zsoldier
Expert
Expert

It might be easier to use the customization specifications in vCenter to do what you are trying to do.  It's essentially the same thing.

The way you are describing could be done, it would just require a bit of a round about method.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
DTsaliabine
Contributor
Contributor

Hi,

Thanks for answer!

I'll try (as I know that should work fine), I just want to reduce time (all templates are ready and configured, the only I need to obtain VM name Smiley Happy)

0 Kudos
mattboren
Expert
Expert

Hello, DTsaliabine-

Ah, I see -- you are not after the guest OS's host name, but rather the name of the VM object.  Like Zsoldier said, using an OSCustomizationSpec might be helpful in this situation.

But, as a couple of examples of getting the VM object's name by running a script within the VM, here are a couple of snippets:

## using standard PowerCLI cmdlet, Get-VM (not very fast, esp. in a larger environment)
(Get-VM | ?{$_.ExtensionData.Guest.HostName -eq $env:Computername}).Name

Or, a way that should run faster:

## get the Name property of the .NET View object of the VM where Tools reported the given computer name (the guest's "hostname")
(Get-View -ViewType VirtualMachine -Property Name -Filter @{"Guest.HostName" = "^$($env:Computername)$"}).Name

VMware Tools seem to report the "DNS Name" of the guest OS as its short name when the machine is not on a domain.  So, you could use just the short name in a Where-Object clause or as a filter for getting the .NET View object for the corresponding VM in vCenter, and then access the Name property to get the actual VM object's name.
These snippets assume that VMware Tools are running in the VM, that you have PowerShell and PowerCLI on the VM, and some credentials stored there to make the connection to vCenter.
0 Kudos