Book Image

Learning PowerCLI for VMware VSphere

By : Robert van den Nieuwendijk
Book Image

Learning PowerCLI for VMware VSphere

By: Robert van den Nieuwendijk

Overview of this book

Table of Contents (17 chapters)
Learning PowerCLI
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using COM objects


You can also use the New-Object cmdlet to create an instance of a COM object. COM objects were used a lot in VBScript. In PowerShell, you can still use them to do things that you cannot do in native PowerShell. The following example will use the SAPI.SpVoice COM object to output a text as voice. It will say "The script is finished." Append this piece of code at the end of your PowerCLI script and you will hear your computer say that the script is finished. This way, you don't have to keep watching your computer screen. Isn't this cool?

PowerCLI C:\> $Voice = New-Object -ComObject SAPI.SpVoice
PowerCLI C:\> $Voice.Speak("The script is finished.") | Out-Null

You can get a list of all of the COM objects on your computer with the following PowerShell code:

Get-ChildItem -Path HKLM:\Software\Classes -ErrorAction ` SilentlyContinue |
Where-Object {$_.PSChildName -match '^\w+\.\w+$' –and `
  (Get-Itemproperty "$($_.PSPath)\CLSID" -ErrorAction ` SilentlyContinue)} |
Format...