Skip to content

August 31, 2011

VMworld: vSphere PowerCLI Best Practice (VSP1883)

Speakers: Luc Dekens (Eurocontrol), Alan Renouf (VMware)

Luc – blog: http://lucd.info
Alan – blog: http://www.virtu-al.net

BP1: Get-View returns full copy of the server-side object
– otherwise the get- commands only return a subset of the properties

BP2: Finding Properties
– what you see is not what is there
– – can edit the .ps1xml files to change returned properties
– use Get-Member to return all of the properties
– ex. Get-VMHost | Get-Member
– complex (nested) objects
– – Select-Object -ExpandProperties
– – Get-Member in a loop
– – Format-Custom Depth

BP3: Make Your Own Properties
– use the New-VIProperty cmdlet
– adds a CodeProperty to the object
– valid until Remove-VIProperty or end of session


BP5: Variable # of Properties
– create with Add-Member
– with C# struct
– define an entirely new object
– you can Add-Type but not Remove-Type

BP6: Export-CSV & variable # of properties
– first row is used to determine the number of columns
– row two until ‘n’ can be missing columns in the CSV
– solution: descending sort on # of columns
– perhaps not your desired sort order

(Properties demonstration)

BP6: Use Get-View for speed
– bigger environments – use Get-View
– Get-VMHost | Select Name,PowerState
– Get-View -ViewType HostSystem | Select Name, @{N=”PowerStae”;E={$_.Runtime.PowerState}}

BP7: Use the Property parameter
– select only the properties you want
– decide between node or leaf
– go as deep as possible
– will make your command a lot faster

BP8: Know the Get-View filter
– faster to filter on Get-View
– hash table with a RegEx expression value
– names with RegEx meta characters
– need to RegEx escape characters
– filter goes at the end with nothing following

BP9: Use the linked list
– adding a row to a list, recreates the list
– uses a pointer to the array list object

(Speed demonstration)
– Get-VM runs on client-side to get results
– Get-View runs on server-side with 15x speed increase

BP10: Reporting
– standard 3-part layout
– simple example – one-liner
– – …ExportCsv…

BP11: Use the PS force I
– Sort-Object
– Group-Object
– – great for handing statistical data
– – Get-Stat -Entity (Get-VM) -Stat…

BP12: Uss the PS force II
– calculated properties allow all kinds of manipulations (start with “@”)
– use the -format operator
– all the .Net methods are available

BP13: Using Recursive Functions
– simplify your scripts with recursive functions
– running through a tree structure (ex. vCenter)
– reaching the 1000-limit mostly indicates a script problem

BP14: Schedule your scripts
– use Windows Scheduler
– you can use the Schedule.Service COM object
– PowerShellPack contains a TaskScheduler module
– – works in W2k8/R2/7
– – *** typo in Register-ScheduledTask
– – see Alan’s vCheck script on his blog

BP15: Scheduled script authentication
– the script runs under the RunAs account
– – TaskScheduler module -Credential parameter
– watch out: different PS profile(s), vSphere privileges, leave no interaction in your script

BP16: Email your report
– PS v2: Send-MailMessage

BP17: Produce HTML reports
– bring some color to your reports
– use style sheets (see vCheck script)
– provide interactive reports

BP18: Email HTML reports
– PS v2: BodyAsHtml

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments