VMware Communities > Blogs

Blog Posts

John Tuffin's Blog

11 Posts
0

Query ESX Time

Posted by cpqarray VMware 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>

http://Reflection.Assembly::LoadWithPartialName("vmware.vim")

$svcRef = new-object VMware.Vim.ManagedObjectReference

$svcRef.Type = "ServiceInstance"

$svcRef.Value = "ServiceInstance"

$serviceInstance = get-view $svcRef

$datetime = $serviceInstance.ServerClock

*
Write-Host $datetime

*

0 Comments Permalink
0

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.

0 Comments Permalink
0

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)

0 Comments Permalink
0

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

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

0 Comments Permalink
0

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.

0 Comments Permalink
0

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).guest.net) {$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

0 Comments Permalink
0

PowerShell and VMware Server

Posted by cpqarray VMware Apr 8, 2008

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.

0 Comments Permalink
1

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

1 Comments Permalink
2

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>

2 Comments Permalink
0

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 = "sc-gallium04.pso.vmware.com"

$vSwitch = "vSwitch1"

$user = "scriptuser"

$password = "vmware"

$VC = "10.18.138.187"

*
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

$net.AddPortgroup($PortGroupSpec)

0 Comments Permalink
4

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

Get-VIServer 10.18.138.187 -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.

4 Comments Permalink