Part two in Some Advice on Creating Powershell Scripts

Now that you have found all sort of interesting properties, you might feel the urge to modify one or two. After all, creating slick overviews is cool, but bricking your entire infrastructure with one script is … well, you get the idea.

There are generally two possible ways of modifying stuff using Powershell:

1. The easy way
Use the cmdlet, that’s wat it’s for! That people that brought you Get-Something, might also have created Set-Something for you convenience. But how should I know?
Use Get-Command “*Something*” to find it! Or, for a list of all the stuff you can Set, type Get-Command -Verb Set. Then take a peek at Help Set-Something -Detailed for all the stuff you need to know about using the cmdlet. Happy Setting!

2. The hard way
Some properties might not be so easy to change. Maybe because they were to lazy to create a Set-Something for you; maybe because they don’t want you to be messing with it! But you have to go and change it anyway, don’t you? I know how you feel. Luckily, METHODS allow you to execute specific actions. Here’s how to explore methods:

2.1. Finding the method you need: Use Get-Member on an object you’d like to modify. (E.g.: $VMHostView = Get-VMHost HOSTNAME | Get-View ; $VMHostView | Get-Member)

2.2. Finding the syntax of the method: Type it without brackets (E.g.: $VMHostView.Rename). It will show some info including the OverloadDefinition, e.g.: System.Void Rename(String newName)
Ignore the System.Void, note that String is the type of the parameter and newName is the name of the parameter. In case of VMware VI Toolkit Methods, you could search for the method in the API reference for more info on the parameters e.g. their properties.

2.3. Create the parameter objects. In the example, a string is easy to create, just use double quotes.($param = “newname”) In other methods it might state LicenseSource or any other kind. For VI Toolkit stuff, use $param1 = New-Object VMware.Vim.TypeName (e.g.: VMware.Vim.LicenseSource). When looking at your new object, it will already have all properties, but with no values. Set the values you want and then use the object as input for your method, e.g.: $VMHostView.Rename($param)

If you use the wrong type of object or the incorrect amount of parameters, read the error message carefully. It will tell you what went wrong. You can always copy to method name into Google and see if it returns any useful examples.

Hope this is helpful. Let’s start the discussion in the comments section.

Hugo

»crosslinked«

No related posts.

  2 Responses to “Some Advice on Creating Powershell Scripts (Part 2)”

  1. Thats amazing, thanks very much for the explanation.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2012 PeetersOnline Suffusion theme by Sayontan Sinha