Archive for the ‘PowerShell’ Category

Suppress “Supply values for the following parameters: Credential” when using Get-Credential in PowerShell

August 4, 2011 1 comment

When you want to ask the user for alternative credentials, the Get-Credential cmdlet is your friend.

You get a nice popup, asking you for a username and password.


But your PowerShell console always displays this nasty message:


Read more…

Categories: PowerShell

How to Hard Kill a stuck VM with PowerCLI

February 9, 2011 2 comments

Sometimes a VM is stuck and can’t be killed with vSphere Client (or from within the VM).

On ESX, you could login and perform the famous kill –9 <PID> to kill the VM.  Most people don’t realize that this functionality has been added to PowerCLI in version 4.1 Update 1.

First, connect directly to the ESX(i) host.

Connect-VIServer -Server <Hostname> -Credential $(Get-Credential -Credential root)

Read more…

Categories: PowerCLI, PowerShell, vSphere

PowerCLI: Convert PortWorldWideName or NodeWorldWideName to hexadecimal format.

February 9, 2011 Leave a comment

If you output one of the fields of $VMHost.Config.StorageDevice.MultipathInfo.Lun[x].Path[x].Transport, you will get the output in standard decimal numbers.

This is different from what is used in SAN environment where they mostly use XX:XX:XX:XX:XX:XX:XX:XX where XX is a hexadecimal number.


Use the following code to convert from the decimal format to the hexadecimal SAN format:

[String] $lstrWWNNHex = "{0:x}" -f $NodeWorldWideName
[String] $lstrWWNNHexFormatted = ""
For ($i=0;$i -lt 8;$i++)
$lstrWWNNHexFormatted += "{0}:" -f $($lstrWWNNHex.SubString($i * 2, 2))
$lstrWWNNHexFormatted = $lstrWWNNHexFormatted.SubString(0, $lstrWWNNHexFormatted.Length - 1)

$NodeWorldWideName is the WWN generated by $VMHost.Config.StorageDevice.MultipathInfo.Lun[x].Path[x].Transport.NodeWorldWideName.  You can use the same for PortWorldWideName.

Categories: PowerCLI, PowerShell, vSphere

How to check if PowerCLI libraries are loaded in PowerShell/PowerGUI

February 3, 2011 2 comments

If you write scripts in PowerShell or PowerGUI, it might be handy to check if the PowerCLI libraries are loaded.

Even though you have PowerCLI installed, this does not mean the objects are loaded in your PowerShell session.

In PowerGUI for example, you can check this by clicking File – PowerShell Libraries.


Read more…

Get the FQDN of your host with PowerShell

December 31, 2010 16 comments

I was looking into a way to get the FQDN of my server in PowerShell.  There are some Environment Variables (like USERDNSDOMAIN), but they reflect the current logged on user and not the computer.


Use the following code to list your FQDN:

$objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()

"{0}.{1}" -f $objIPProperties.HostName, $objIPProperties.DomainName


Categories: PowerShell

Create an ‘Internal’ vSwitch with PowerCLI

October 27, 2010 1 comment

I was looking for the correct syntax to create a vSwitch with PowerCLI which has no vmnics attached (aka: Internal vSwitch).


I tried to run the following cmdlet.  The pNIC’s are specified with the –Nic parameter.

Get-VirtualSwitch -VMHost "ServerName" -Name "vSwitchName" | Set-VirtualSwitch -Nic "" -Confirm:$false

But this resulted in a nice error 🙂


Read more…

How to use Try – Catch in PowerCLI the easy way

October 13, 2010 Leave a comment

Sometimes you need to check if something exists.  For example, let’s check if a specific user exists on our ESX host (you need to connect directly to the ESX host for this example to work).


We do this by running:

Get-VMHostAccount -Id UserName

Read more…

Categories: PowerCLI, PowerShell, vSphere