Using Alternate Credentials with WMI and PowerShell - Take 2 [Hyper-V]
Last week I did a post about specifying alternate credentials to use with WMI and PowerShell for controlling Hyper-V. Well – thanks to some tips and tricks given to me by my readers – here is a new an improved version:
# Setup variable for optional commandlet parameters
$extraParams = @{}
# Setup parameters for prompt to ask if the user wants to use a remote computer
$message1 = "Do you want to use a remote computer - or the local system?"
$local = New-Object System.Management.Automation.Host.ChoiceDescription "&Local", "Connect to the local computer."
$remote = New-Object System.Management.Automation.Host.ChoiceDescription "&Remote", "Connect to a remote computer."
$options1 = [System.Management.Automation.Host.ChoiceDescription[]]($local, $remote)
# Ask the user if they want to use a remote computer
$result1 = $host.ui.PromptForChoice($null, $message1, $options1, 0)
if ($result1 -eq 1)
{
# Prompt for the Hyper-V Server to use
$extraParams["computername"] = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Setup parameters for prompt to ask if the user wants to use alternative credentials
$message2 = "Do you want to use alternative user credentials?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Use alternative user credentials."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Use your current user credentials."
$options2 = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
# Ask the user if they want to use alternative credentials
$result2 = $host.ui.PromptForChoice($null, $message2, $options2, 0)
if ($result2 -eq 0)
{# Prompt for the user credentials to use
$extraParams["credential"] = Get-Credential -credential $null
}
}
# Get all guest KVP data objects on the server in question, using optional parameters
$Kvps = gwmi -namespace root\virtualization Msvm_KvpExchangeComponent @extraParams
# Create an empty hashtable
$table = @{}
# Go over each of the guest KVP data objects
foreach ($Kvp in [array] $Kvps)
{
# Get the OSName value out of the guest KVP data
$xml = [xml]($Kvp.GuestIntrinsicExchangeItems | ? {$_ -match "OSName"})
$entry = $xml.Instance.Property | ?{$_.Name -eq "Data"}
# Filter out unknown operating systems
if ($entry.Value) {$value = $entry.Value} else {$value = "Unknown"}
# Count up the values and store it in a hashtable
if ($table.ContainsKey($value))
{$table[$value] = $table[$value] + 1 }
else
{$table[$value] = 1}
}
# Display the results in a nicely formated and sorted manner
$table.GetEnumerator() | Sort-Object Name | Format-Table -Autosize
There are two key changes:
- I now only prompt for alternative credentials if you first indicate that you are connecting to a remote computer.
- Optional parameters for the “Get-WMIObject” command are now stored in the $extraParams variable. This is really handy as it means you do not need to special case each call to get a WMI object.
Cheers,
Ben