Skip navigation

One of the features about PowerCLI 4 Update 1 that I really like is the ability to read credentials from a credential store. A couple of new cmdlets really come in handy when you use the credential store.


New-VICredentailStoreItem and Get-VICredentialStoreItem


Rather than embed passwords in a script or stopping execution of the script so the user can enter the credentials, New-VICredentialStoreItem allows you to store the user name and password in an obfuscated XML file.


For example in my vCenter Server system named vc1.vmware.local my user name is admin and my password is vmware. I can store the credentials in a file with the New-CredentailStoreItem cmdlet.


New-VICredentialStoreItem -Host vc1.vmware.local -User admin -Password vmware -File C:\credentials.xml


The file is not a secure password repository and anyone that has access to the file has the ability to recover the passwords that are stored there.  Still it does avoid having to re-enter the user name and password over and over again. 































This example creates a new entry for the host in the file. If the file does not exist then the file is created. To read Credentials from the file, you use the Get-VICredentialStoreItem cmdlet.




$c = Get-VICredentialStoreItem -Host vc1.vmware.local -User admin -Password vmware -File C:\Credentials.xml




Now you have the credentails stored in the variable, you can use them in your connect cmdlet.




Connect-ViServer vc1.vmware.local -User $c.User -Password $c.Password




Ok a couple of warnings about this credentail file, it's not designed to be a secure password repository. Though the passwords in the file are obfuscated, anyone who has rights to read the file has the ability to recover the passwords from the file.

































Posted by cpqarray Jul 21, 2008

I've been playing around with the get-stat command. Thanks a bunch to Brian Denicola at for posting the example.


To grab memory stats from an ESX host type the command get-stat $esx -Memory -maxsamples 3 -realtime this will grab 3 memory samples from the server $esx.



For example



     $vc = get-viserver myvcserver -username admin -password vmware
     $esx = get-vmhost MyEsxServer
     $get-stat $esx -Memory -maxsamples 3 -realtime

The -common switch gives averages for all major hardware subsystems cpu,memory, disk, and networking.


$get-stat $esx -common -maxsamples 1 -realtime

There is also a way to pull individual counters provided that you know the counter name


get-stat $esx -stat cpu.usagemhz.average

This returns the average cpu usage in MHZ over the interval of collection. The list of all counters can be found here:




This is cool becasue so often I've wanted to modify the boot order of a virtual machine and this little bit of code allows you to do it.  I can't remember where I found it now so I can't give credit but thanks to the poster that put this up in the forums.









$spec = New-Object VMware.Vim.VirtualMachineConfigSpec $spec.extraConfig += New-Object VMware.Vim.OptionValue $spec.extraConfig[0].key = "bios.bootDeviceClasses" $spec.extraConfig[0].value = "allow:cd,hd" (get-view (Get-VM -Name <VM-name>).ID).ReconfigVM_Task($spec)




Query ESX Time

Posted by cpqarray May 14, 2008

If you're not reading the forum for the VMware VI Toolkit Beta you are missing out on just a goldmine of useful information. Take this post from LUCD. This will allow you to query the ESX host for it's current time. This another one of those instances where there is no applet in the toolkit to do this so you hook into the API to get the information.



Get-VIServer -Server  <Replace with yourServer Name>




$svcRef = new-object VMware.Vim.ManagedObjectReference



$svcRef.Type = "ServiceInstance"



$svcRef.Value = "ServiceInstance"



$serviceInstance = get-view $svcRef



$datetime = $serviceInstance.ServerClock



Write-Host $datetime





I found this example in the VMworld Europe presentation (excellent by the way) that would allow you to move a Virtual Machine from one virtual switch to another.






get-vm vm1 | set-networkadapter "Production"



Now say you wanted to move all vm's from the "VM Network" to "Production"



get-vm | where { ($_ | get-networkadapter).NetworkName -eq"VM Network} | set-networkadapter -networkname "Production"



This would move all VM's from the VM Network to Production.






Here is how you can use PowerShell to create a virtual machine:






get-vmhost ESX1 | new-vm -name `



"First VM User N" -memorymb 256 `



-diskmb 1024 -pool `



(get-resourcepool studentN-esx1) `



-location (get-folder studentN)



Returning the product information about the ESX host with a simple command


Get-VMHost | % { (Get-View $_.ID).Config.Product }



Thanks to LucD on the forum for showing me this little trick today. To prompt for a username and password in get-viserver pass a -credentials argument to the command. For example


get-viserver <Virtual Center Server Name> -Credentials (get-credential)


that will prompt for a username and password rather than having to put it into the script.








I found this little bit code useful on the forums. It returns a list of virtual machines and their mac addresses.


<code>Get-VM | select name, @{Name="MAC"; expression={foreach($nic in (Get-View $_.ID) {$nic.macAddress}}} </code>






Note that the forums are putting the code tags in front of and at the end of the line of code.   That is not part of the actual code that you run in Powershell



I just discovered that the Vmware VI Toolkit for Windows will work with VMware Server 2.0 Beta 2 as well with Virtual Infrastructure. I was able to connect with the command:


get-viserver <VMware Server Name> -port 8333 -user <User Name> -password <Password>



Then I was able to run the power commands as well as the new-snapshot command.



I found this while surfing the forums this little bit of code will place an ESX Host in maintenance mode. I wonder what other values might exist for the -State switch?






Get-VMHost -Name <hostname> | Set-VMHost -State maintenance



This bit of code takes the output from Get-Datastore and converts it from megabytes to Gigabytes. It also labels the columms as FreespaceGB and CapacityGB.   This is from the forums as well. 






<code>Get-Datastore | ft name,@{ Label = "FreespaceGB"; Expression = { $_.FreeSpaceMB * 1MB / 1GB } }, @{ Label = "CapacityGB"; Expression = { $_.CapacityMB * 1MB / 1GB } } </code>



This little snippet of PoweShell Code will create a virtual switch on an ESX Host. I found this while hunting through the VMware comunities for why I couldn't create a portgroup with Powershell. Using this code I was able to create a Virtual Switch called vSwitch1 with a VM portgroup called Production. I still have no idea what the policy setting does or do I know of any other values that could go there.


$ESXhost = ""


$vSwitch = "vSwitch1"


$user = "scriptuser"



$password = "vmware"



$VC = ""



Get-VIServer $VC -User $user -Password $password




$NewSwitch = New-VirtualSwitch -vmhost (Get-VMHost $ESXhost ) -Name $vSwitch -NIC "vmnic1"



$net = Get-View(Get-View(Get-VMHost -Name $ESXHost).ID).configmanager.networksystem



$PortgroupSpec = New-Object vmware.Vim.hostportgroupspec



$PortgroupSpec.vswitchname = $vSwitch



$PortgroupSpec.Name = "Production"



$PortgroupSpec.policy = New-Object vmware.Vim.HostNetworkPolicy






These two lines of Powershell script are all you need to VMotion all virtual machines from one ESX host to another:


Get-VIServer -User "scriptuser" -Password "vmware"


Get-VMHost sc-gallium07 | Get-VM | Move-VM -Destination (Get-vmhost sc-gallium04)











If you have Powershell and the VI-toolkit installed on a Windows box you run this little script and you have just scripted a VMotion.