Below you will find pages that utilize the taxonomy term “PowerShell”
Posts
PowerCLI Move-Datastore Function
Moving datastores into folders via drag/drop can be painful. In some cases vCenter does not want to allow a drag from a long list. Here is a quick function to make moving via PowerCLI a little bit easier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Function Move-Datastore { param ( [VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.VmfsDatastoreImpl[]] $datastore = $(throw "Datastore(s) must be provided.
Posts
Convenience Functions for Connecting to Multiple vCenters
In a large environment repeatedly connecting and disconnecting from groups of vCenters can be tedious so I created some helper functions and put them in my PowerShell profile.
1 2 3 Connect-VIServerGroup lab Connect-VIServerGroup desktops Disconnect-VIServerGroup lab 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Function Connect-VIServerGroup { param ( [String]$group = $(throw "A VI server group name must be specified.
Posts
Single pane for multiple vCenter alarms
When you have multiple vCenters it can be a pain to go through each one checking for alarms. Hence the script below. You can specify the vCenters you want to connect to and it will display a nice sortable/filterable grid view of all of the triggered alarms.
# Usage .\Get-TriggeredAlarms -vCenters vc001,vc002 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 param ( [String[]]$vCenters ) Function Get-TriggeredAlarms { param ( $vCenter = $(throw "A vCenter must be specified.
Posts
PowerShell: Alternate script behavior when dot sourced vs run directly
I use dot sourcing in my PowerShell scripts to reuse functions* and sometimes I want different behavior if they are included (dot sourced) or run directly. This concept is used in Python to provide an easy way to run tests against the code when run directly but not when it is imported.
You are not restricted to running tests using this approach, but I think that automated testing is useful so that is the example I provide below.
Posts
Validate datastore visibility between hosts
This script will check a set of hosts and report on whether the available datastores match. The first host found becomes the reference host and the others are compared against it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 param ( [VMware.
Posts
Find LUNs Presented to Multiple Clusters
After some cluster migrations it appears that not all of the LUN presentations got cleaned up. I needed to find the LUNs that were shared by different clusters so I could determine which one they should belong to and remove access for the other cluster(s). PowerCLI to the rescue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 param ( [VMware.
Posts
Validate Username / Password on ESX(i) Host
A simple script to validate the login credentials against an ESX(i) host. Useful when validating that the credentials are what you think they should be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 <# .
Posts
VMware PowerCLI 5 Cmdlets by Noun
Generated using this post.
<th> Verbs </th> <td> Get,New,Remove,Set </td> <td> Get,New,Remove </td> <td> Get,New,Remove </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Copy </td> <td> Apply,Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get </td> <td> Copy,Get,New,Remove,Set </td> <td> Get,Move,Remove </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Set </td> <td> Get </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,Stop,Wait </td> <td> Get,Move,New,Remove,Set </td> <td> Dismount,Mount,Update,Wait </td> <td> Get,Remove </td> <td> Export,Get,Import,Move,New,Remove,Set,Start,Stop </td> <td> Get </td> <td> Get,New,Remove </td> <td> Get </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,New,Remove </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Connect,Disconnect </td> <td> Get,Move,New,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,Restart,Shutdown,Suspend </td> <td> Copy </td> <td> Get,Set </td> <td> Get,New,Remove </td> <td> Add,Get,Move,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get </td> <td> Get,Set </td> <td> Get </td> <td> Format,Get </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Install </td> <td> Apply,Export,Get,Import,New,Remove,Set </td> <td> Test </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Restart,Set,Start,Stop </td> <td> Get,Set,Test </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Invoke </td> <td> Get,Set </td>
Posts
Simple XML output of a data object
I am doing some data collection using PowerShell and the output needs to be a simple XML document string.
The built in cmdlets do a great job when persisting data, but they produce some fairly complicated XML that provides more detail on object types than I can use.
Here is a single function to take a PSObject based data object and turn it into XML. I have also included a test function to show an example of building the data object.
Posts
PowerShell Asynchronous Job Queueing
Here is some sample code to have a variable size work queue for asynchronous jobs in PowerShell. The sample code uses Start-Job, but it was written as a skeleton to be able to run multiple PowerCLI tasks at once.
The variable length is beneficial in case the work should be split up, but there is a chance of overwhelming the system if too many concurrent jobs are started at once.
Posts
Basic PowerShell Registry Value Helpers
Working with registry values in PowerShell can be a little cumbersome, so here are some example helpers that I am using to simplify some of my configuration scripts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 Function Get-RegistryValue { param ( $key, $value ) (Get-ItemProperty -Path $key -Name $value).
Posts
Using PowerCLI to Uninstall vCenter Extensions
After finishing a CapacityIQ evaluation we wanted to uninstall the plug-in from vCenter. VMware KB article 1025360 shows how to do this, but it requires the Managed Object Browser. Per the security hardening guide we disabled the mob so this solution would not work as given. PowerCLI to the rescue.
Here are the steps given in the original article and how they translate into PowerCLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #1.
Posts
VMware PowerCLI 4.1.1 Cmdlets by Noun
Generated using this post.
<th> Verbs </th> <td> Get,New,Remove,Set </td> <td> Get,New,Remove </td> <td> Get,New,Remove </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Copy </td> <td> Apply,Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get </td> <td> Copy,Get,New,Remove,Set </td> <td> Get,Move,Remove </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Set </td> <td> Get </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,Stop,Wait </td> <td> Get,Move,New,Remove,Set </td> <td> Dismount,Mount,Update,Wait </td> <td> Get,Remove </td> <td> Export,Get,Import,New,Remove,Set,Start,Stop </td> <td> Get,New,Remove </td> <td> Get </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> New,Remove </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Connect,Disconnect </td> <td> Get,Move,New,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,Restart,Shutdown,Suspend </td> <td> Copy </td> <td> Get,Set </td> <td> Get,New,Remove </td> <td> Add,Get,Move,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get </td> <td> Get,Set </td> <td> Get </td> <td> Format,Get </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Install </td> <td> Apply,Export,Get,Import,New,Remove,Set </td> <td> Test </td> <td> Get,New,Remove,Set </td> <td> Get,Restart,Set,Start,Stop </td> <td> Get,Set,Test </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Invoke </td> <td> Get,Set </td>
Posts
PowerCLI Presentation from Central Ohio VMUG
Here is an introduction to PowerCli presentation I gave back in July at the Central Ohio VMware User’s Group.
Posts
VMware PowerCLI 4.1 Cmdlets Summarized by Noun
Here is the list of VMware PowerCLI cmdlets ordered by noun. I find this easier to use when I am trying to write scripts. You can locate what items you want to work with and then see the action you can take. This post contains the code used to generate the list.
<th> Verbs </th> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Copy </td> <td> Apply,Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Move,New,Remove,Set </td> <td> Copy,Get,New,Remove,Set </td> <td> Get,Move,Remove </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Set </td> <td> Get </td> <td> Get,Move,New,Remove,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> Get,Stop,Wait </td> <td> Get,Move,New,Remove,Set </td> <td> Dismount,Mount,Update </td> <td> Get,Remove </td> <td> Export,Get,Import,New,Remove,Set,Start,Stop </td> <td> Get,New,Remove </td> <td> Get </td> <td> Get </td> <td> Get </td> <td> Get,New,Remove,Set </td> <td> Get </td> <td> New,Remove </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Get,New,Remove,Set </td> <td> Connect,Disconnect </td> <td> Get,Move,New,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,Restart,Shutdown,Suspend </td> <td> Copy </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Move,Remove,Restart,Set,Start,Stop,Suspend </td> <td> Get,New,Remove,Set </td> <td> Get,Set </td> <td> Get </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,New,Remove,Set </td> <td> Add,Get,Remove </td> <td> Get,Install </td> <td> Apply,Export,Get,Import,New,Remove,Set </td> <td> Test </td> <td> Get,New,Remove,Set </td> <td> Get,Restart,Set,Start,Stop </td> <td> Get,Set,Test </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Get,Set </td> <td> Invoke </td> <td> Get,Set </td>
Posts
Quick and dirty PowerPath/VE output parser
Here is a quick and dirty PowerShell script to parse the output of the rpowermt command used to manage PowerPath/VE on ESX (for EMC arrays). I wrote the code quickly to solve a particular problem, but if I get requests I could extend it to parse the rest of the output and do so in a little more robust fashion.
Why do this? I am in the middle of migrations between frames and I needed an easy way to determine what needed moved when the storage admin said move all LUNs from CLARiiON XXXX to the new LUNs on YYYY – and make sure to move LUN 41 to LUN 88.
Posts
VMware PowerShell Get-VIHumanReadablePath
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Function Get-VIHumanReadablePath { param ( $target ) try { if ($target.
Posts
Another Orchestrator/PowerShell hang issue
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).
Posts
Running PowerShell in VMware Orchestrator – updated
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.
Posts
Adding VMware PowerCLI to your standard PowerShell environment
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.
1 2 3 4 5 6 # 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.
Posts
Export and Import Customization Specs Using PowerCLI
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Function Export-OSCustomizationSpec { param ( [string]$specName, [string]$exportFile = "$specname.
Posts
Notify All Active vSphere Users (PowerCLI version)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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.
Posts
Summarize PowerCLI Cmdlets by Noun
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $commandTable = @{} $commandList = Get-Command -Module VMware.VimAutomation.Core -CommandType Cmdlet foreach ($command in $commandList) { if (!
Posts
Bulk password change on ESX hosts
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.
The script has some documentation in it so make sure to use Get-Help to find more detail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Get-Help .
Posts
PowerShell advanced parameter behavior unexpected
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.
The standard way of using a pipeline parameter is to do something like this:
param ( [parameter(Mandatory=$true, ValueFromPipeLine=$true)] [String[]]$item ) In cases where you accept input from the pipeline or a single item as an input parameter this works fine.
Posts
List VMs with Non Thick Disks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 foreach ($vm in Get-VM) { $badFormat = $false foreach ($hardDisk in (Get-HardDisk -VM $vm)) { if ($hardDisk.StorageFormat -ne "Thick") { $badFormat = $true } } if ($badFormat) { $vm.Name } }
Posts
Determine VMs in a SRM recovery plan using PowerShell
Here is some PowerShell code to determine the VMs that are in a VMware Site Recovery Manager recovery plan.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #requires -version 2 # Description: List the VMs in a SRM recovery plan.