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)
It has come to my attention that some times the scripts will still hang after using the trick from a previous post to fix an issue with standard input when running PowerShell in Orchestrator workflows. After further research there appears to be an issue with stderr hanging the scripts in a similar way to stdin. To be safe you should do something with the output from stderr (where error messages are sent).
You can do at least three things
//Throw it away with 2>NUL
command = new Command("cmd.exe /c powershell.exe -File c:\\orchestrator\\test.ps1 < NUL 2>NUL");
//Log it to a new file with 2> filename
command = new Command("cmd.exe /c powershell.exe -File c:\\orchestrator\\test.ps1 < NUL 2>c:\\orchestrator\\stderr.log");
//Send it wherever standard output goes using 2>&1
command = new Command("cmd.exe /c powershell.exe -File c:\\orchestrator\\estest.ps1 < NUL 2>&1");
For further reading on the topic of standard input, output, and error please see
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
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);
In order to get access to PowerCLI when using another PowerShell shortcut or environment you can run the following commands which is what happens when you run the VMware provided shortcut.
# Adds the base cmdlets
Add-PSSnapin VMware.VimAutomation.Core
# Add the following if you want to do things with Update Manager
Add-PSSnapin VMware.VumAutomation
# This script adds some helper functions and sets the appearance. You can pick and choose parts of this file for a fully custom appearance.
. "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"
Note on the last line that there is a space between the “.” and the path to the script. That space is very important as it means we are including or “dot sourcing” the file.
If you want this all to load every time you launch PowerShell you can add these commands to your profile startup scripts. This is a great place to add functions or aliases that you use all the time. The profile is run once when the PowerShell environment is launched.
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…