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.
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
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.
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.
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 )
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