Over two years ago I discovered PowerShell as a very usefull scripting language to support the fully automated install and config of a Windows 2008 Office Automation domain with XenApp and RES Software products. Thanks to RES Automation Manager (back than it was still called Wisdom) I could easily build PowerShell scripts to create the required Active Directory structure, Exchange environment and use small scripts for different configurational settings. RES Automation Manager ensured I only needed to concentrate on the actual scripts, as I could configure the Tasks options to ensure the correct execution rights were used and the required snap-ins were loaded by specifying the use of a PowerShell configuration file (.psc1).
This blog is all about reconnecting with PowerShell as I want to explore the PowerShell SnapIn for XenServer 6.0.
A quick overview of the available commands is provided with a great poster from Björn Andersson (diversetips.se) for XenServer 6.0:
This blog will be all about the basic PowerShell commands to setup my scripting environment and slowly build up to more complex scripts for XenServer management.
Installing the XenServer PowerShell SnapIn
For directions on the installation of the XenServer SnapIn, you can check out a post I wrote about it a while ago, as 64-bits environments (or environments using UAC, which require the use of an elevated Command Prompt) need some additional steps to register the SnapIn correctly.
So keep in mind two commands to correctly register the SnapIn for use with PowerShell:
- Register the XenServer SnapIn for x86:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe "C:\Program Files (x86)\Citrix\XenServerPSSnapIn\XenServerPSSnapIn.dll"
- Register the XenServer PowerShell SnapIn for x64:
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe "c:\Program Files (x86)\Citrix\XenServerPSSnapIn\XenServerPSSnapIn.dll"
Using the PowerShell SnapIn
So let’s fire up the PowerShell shell and get started with the basics.
* Check the available PowerShell SnapIns:
To check which snapins can be used within your PowerShell environment, you can run the Get-PSSnapin command:
PS C:\Users\admin> Get-PSSnapin -registered Name : XenServerPSSnapIn PSVersion : 2.0 Description : Citrix XenServer PowerShell SnapIn
By checking the registered SnapIns, you can also verify the name used for the snapin, which will be used for reference purposes in other functions.
* Load a SnapIn:
To use the cmdlets from a snapin, you first need to load it with the Add-PSSnapin command:
PS C:\Users\admin> Add-PSSnapin XenServerPSSnapIn
* Check the available cmdlets:
To check if the snapin is correctly loaded, or just to see a list of the available cmdlets provided by a snapin or module, you can use the Get-Module command:
PS C:\Users\admin> Get-Command -Module XenServerPSSnapIn CommandType Name Definition ----------- ---- ---------- Cmdlet About-XenServer About-XenServer [-InstallLocation] [-Vers... Cmdlet Add-XenServer:Bond.OtherConfig Add-XenServer:Bond.OtherConfig [[-Bond] <... ... Cmdlet Set-XenServer:VMPP.NameLabel Set-XenServer:VMPP.NameLabel [[-VMPP] <Ob... Cmdlet Wait-XenServer:Task Wait-XenServer:Task [[-Task] <Object>] [-...
Using a config file to preconfigure the PowerShell environment
For those of us, that would prefer to preload the required PowerShell SnapIns, it is possible to create a PowerShell configuration file (.psc1) and start the PowerShell Prompt with the preloaded SnapIns.
For the XenServer SnapIn you can use the predefined XenServerPSSnapIn.psc1 file, located at C:\Program Files (x86)\Citrix\XenServerPSSnapIn, which contains the following settings:
<?xml version="1.0" encoding="utf-8"?> <PSConsoleFile ConsoleSchemaVersion="1.0"> <PSVersion>1.0</PSVersion> <PSSnapIns> <PSSnapIn Name="FindStringPSSnapIn" /> </PSSnapIns> </PSConsoleFile>
If you want to preload these settings, you can use the -PSConsoleFile option with powershell.exe to start a PowerShell Prompt with the predefined configuration and have the SnapIn automatically loaded and ready for use.
I use the following shortcut to automatically start a preconfigured PowerShell environment, as a shortcut was not automatically created when I installed the XenServer SDK for PowerShell:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files (x86)\Citrix\XenServerPSSnapIn\XenServerPSSnapIn.psc1"
I know this blog only offers some basic PowerShell startup information, but I do intend to share more scripts & tips as I re-discover the scripting fun again. So stay tuned for more PowerShell fun!