Archive

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.

image

But your PowerShell console always displays this nasty message:

image

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.

image

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

Done!

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 🙂

image

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

Add license to a vSphere host

October 13, 2010 Leave a comment

This is a rather short PowerCLI post.

Licensing is not directly available in PowerCLI cmd-lets, so we’ll have to reach out to the SDK, which is easily accessible from PowerShell.  We’ll have to fetch a LicenseAssignmentManager to set the license, so this mean using quite some Get-View cmdlets.  These could all be combined into a single line of code but makes it hard to read.

$strVMHostName is the name of your ESX(i) server to license

$strLicense is your licensekey.  All zero’s like in the example sets the host to evaluation mode.

 

Enjoy!

$strVMHostName = "HostName.domain.com"

$strLicense = "00000-00000-00000-00000-00000"

 

$objServiceInstance = Get-View -Id ServiceInstance -Property Content.LicenseManager

$objLicenseManager = Get-View -Id $objServiceInstance.Content.LicenseManager -Property LicenseAssignmentManager

$objLicenseAssignmentManager = Get-View -Id $objLicenseManager.LicenseAssignmentManager

$objVMHost = Get-View -ViewType "HostSystem" -Filter @{Name=$strVMHostName} -Property Config.Host

$objLicenseAssignmentManager.UpdateAssignedLicense($objVMHost.Config.Host.Value, $strLicense, $null)

 

Categories: PowerCLI, PowerShell, vSphere

How many Powered On VMs are running on my host + Speed up your PowerCLI cmdlets!

August 9, 2010 6 comments

A rather simple script today, but we’ll discuss some nice improvements on our way…

 

Let’s say you want to know how many VMs are running on your host.  The following code will do the trick:

Get-VMHost "host" | Get-VM | Where-Object `

 {$_.PowerState -eq "PoweredOn"} | Measure-Object

It will give you a nice output like this:

image

Allright!  Script works…  but it’s rather slow.  Let’s use the Measure-Command cmdlet to see how long it actually takes in my environment.  The Measure-Command take a ScriptBlock argument at the –Expression parameter.  So pass the whole code as a scriptblock and you know how long it takes to run it.

Read more…

Categories: PowerShell, vSphere