An example of how to get the human readable path of an object in the VMware SDK. This is using PowerShell and requires that PowerCLI be installed, but should be easily converted to other languages.
Function Get-VIHumanReadablePath
{
param
(
$target
)
try
{
if ($target.GetType().BaseType -ne [VMware.Vim.ManagedEntity])
{
$target = $target | Get-View -ErrorAction SilentlyContinue
}
}
catch
{
$target = $null
}
if (!$target)
{
throw "Get-VIHumanReadablePath: Could not get the Managed Entity representation of -target"
}
$path = $target.Name
while ($target.Parent)
{
$target = Get-View $target.Parent
$path = $target.Name + "/" + $path
}
$path
}
Example usage
Get-VIHumanReadablePath (Get-Folder Linux)
Get-VIHumanReadablePath (Get-VM LinuxTemp01)
Get-VIHumanReadablePath (Get-VM LinuxTemp01 | Get-View)
Get-VIHumanReadablePath (Get-ResourcePool Prod)
Get-VIHumanReadablePath (Get-VM LinuxTemp01 | Get-ResourcePool)
Get-VIHumanReadablePath (Get-VMHost esx001.local)
Updated 7/29/2010 – I have added a new follow up post this post regarding another related hanging issue. Please make sure to check it out.
This is a copy of my comment in the VMware Communities, but some times the Google search results for items in this blog show up and the ones for the communities do not.
The original problem is that PowerShell scripts hang when running them in Orchestrator.
I have spent some time on this and have a way to make it work. For some reason when run this way the $input variable in Powershell is expecting a pipeline and I believe it just waits for pipelined input.
The $input variable is of type System.Management.Automation.Runspaces.Pipeline instead of the standard System.Collections.ArrayList+ArrayListEnumeratorSimple when running scripts. I have tried using the methods in the Pipeline class to clear things up to no avail. I have also tried using the input property on the Orchestrator Command class, but I could not get it to work. I am just getting started with Orchestrator though, so it may be my lack of knowledge.
As a workaround you can input NUL to the script or command using cmd.exe.
// Run a command
command = new Command("cmd /c powershell.exe -Command dir variable: >> c:\\orchestrator\\input.out < NUL");
command.execute(true);
//Run a script
command = new Command("cmd /c powershell.exe -File c:\\orchestrator\\input.ps1 < NUL");
command.execute(true);
These are some examples that I will be reviewing at an upcoming Central Ohio VMware User’s Group.
# List all VMs
Get-VM
# Get one VM
Get-VM ubuntu01
# Get VMs that match
Get-VM ubuntu*
# List VMs in a Datacenter
Get-Datacenter dc001 | Get-VM
# List VMs in a cluster
Get-Cluster cluster001 | Get-VM
# Get 2 CPU or larger VMs
Get-VM | Where-Object {$_.NumCPU -ge 2}
# Get 2 GB RAM or larger VMs
Get-VM | Where-Object {$_.MemoryMB -ge 2048}
# Getting information about what you can do
Get-VICommand
Get-Command
Get-Help Get-VM
Get-Member
Get-VM | Get-Member
# Save a report of VM CPU/MEM configuration information
Get-VM | Select-Object Name, NumCPU, MemoryMB | Export-Csv "VM_CPU_Memory_20100720.csv" -NoTypeInformation
# List VMs with connected CD-ROMs
Get-VM | Where-Object {$_ | Get-CDDrive | Where-Object {$_.ConnectionState.Connected}}
# Disconnect all connected CD-ROMs
Get-VM | Get-CDDrive | Where-Object {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false
# Migrate a template to another host
Get-Template ubuntu | Set-Template -ToVM | Move-VM -Destination (Get-VMHost esx001.lab.local) | Set-VM -ToTemplate -Confirm:$false
# Report on Storage
Get-Datastore | Select-Object Name, FreeSpaceMB, CapacityMB | Export-Csv "Datastores_20100720.csv" -NoTypeInformation
# Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD
Get-Datastore "cla_nonrep_01" | Get-VM | Move-VM -Datastore (Get-Datastore "cla_nonrep_00")
# Storage VMotion ALL VMs from one LUN to another (One at a time)
Get-Datastore "cla_nonrep_01" | Get-VM | ForEach-Object {Move-VM -VM $_ -Datastore (Get-Datastore "cla_nonrep_00")}
# Set all limited CPU reservations to unlimted
Get-VM | Get-VMResourceConfiguration | Where-Object { $_.CpuLimitMhz -ne -1} | Set-VMResourceConfiguration -CpuLimitMhz $null
# Set all limited memory reservations to unlimited
Get-VM | Get-VMResourceConfiguration | Where-Object { $_.MemLimitMB -ne -1} | Set-VMResourceConfiguration -MemLimitMB $null
# Configure a new network on a host
Get-VirtualSwitch -VMHost esx003.lab.local -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095
# Configure a new network on a cluster
Get-VirtualSwitch -VMHost (Get-Cluster cluster001 | Get-VMHost) -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095
# Generate a diagnostic log bundle
Get-Log -VMHost esx001.lab.local -Bundle -DestinationPath c:\hostlogs
# List the entries in vmkwarning
(Get-Log -VMHost esx001.lab.local -Key vmkwarning).Entries
# Find something in a log
(Get-Log -VMHost esx001.lab.local -Key vmkernel).Entries | Select-String "Error"
# What logs are available?]
Get-LogType -VMHost esx001.lab.local
# Find VMs with snapshots (Pretty slow - could be optimized)
Get-VM | Where-Object {$_ | Get-Snapshot}
#Verify advanced settings
# http://wannemacher.us/?p=135
# Change advanced settings
# http://wannemacher.us/?p=104
# Change the root password across multiple hosts
# http://wannemacher.us/?p=304
Here are a couple of functions to help you export and import OS customization specs in vCenter. Be aware that if you do this between vCenters or the certificate changes then you will have to set any encrypted passwords to their correct value after the import.
These functions and the standard *-OSCustomizationSpec cmdlets should allow you to replicate your customization specs between multiple vCenters fairly easily.
Function Export-OSCustomizationSpec {
param (
[string]$specName,
[string]$exportFile = "$specname.xml"
)
$csmgr = Get-View CustomizationSpecManager
if ($csmgr.DoesCustomizationSpecExist($specName)) {
$spec = $csmgr.GetCustomizationSpec($specName)
$csmgr.CustomizationSpecItemToXml($spec) | Out-File $exportFile
}
else {
throw "Spec $specName not found"
}
}
Function Import-OSCustomizationSpec {
param (
[string]$importFile,
[string]$specName #Use to change the spec name from that defined in the file
)
$specXml = Get-Content $importFile
$csmgr = Get-View CustomizationSpecManager
$spec = $csmgr.XmlToCustomizationSpecItem($specXml)
# Change the name if a new one was given.
if ($specName) {
$spec.Info.Name = $specName
}
if ($csmgr.DoesCustomizationSpecExist($spec.Info.Name)) {
throw "Spec $specName already exists."
}
else {
$csmgr.CreateCustomizationSpec($spec)
}
}
Steve Jin recently posted How to Notify All the Active vSphere Users on his DoubleCloud blog. Below are some functions to make this easy to do via PowerCLI.
Function Get-VIServiceMessage {
$sessionManager = Get-View SessionManager
$sessionManager.Message
}
Function Set-VIServiceMessage {
param (
[String]$message = $(throw "Must specify a message.")
)
$sessionManager = Get-View SessionManager
$sessionManager.UpdateServiceMessage($message)
}
Function Remove-VIServiceMessage {
Set-ServiceMessage("")
}
Here is some simple code to generate a reference list of the PowerCLI cmdlet objects(nouns) and what actions(verbs) you can perform on them.
This can come in handy for people new to PowerCLI to show them what the cmdlets will handle for them.
$commandTable = @{}
$commandList = Get-Command -Module VMware.VimAutomation.Core -CommandType Cmdlet
foreach ($command in $commandList) {
if (!$commandTable.ContainsKey($command.Noun)) {
$commandTable[$command.Noun] = @()
}
$commandTable[$command.Noun] += $command.Verb
}
foreach ($command in $commandTable.Keys | Sort-Object) {
$verbs = $commandTable[$command] | Sort-Object
$command | Select-Object @{Name="Noun"; Expression={$command}},
@{Name="Verbs"; Expression={[String]::Join(",", $verbs)}}
}
Here is the output for PowerCLI version 4 update 1.
Read more…
Here is an script to make life a little easier when doing bulk updates to a password for an account as long as the account has a common password on the different hosts.
Read more…
I have been experimenting with parameter values from the pipeline and the processing behavior is slightly different when using piplines vs. parameterized values. It is easy enough to work around, but I wanted to make sure and explain the issue and one workaround.
Read more…
Here is some PowerShell code to determine the VMs that are in a VMware Site Recovery Manager recovery plan.
Read more…