Home > Deployment, PowerShell > How to write PowerShell code part 1 (Using external xml configuration file)

How to write PowerShell code part 1 (Using external xml configuration file)

In this post, I will show you how to use external xml file with PowerShell. The advantage for doing so is that you can avoid other people to open up your PowerShell code to make the configuration changes; instead all they need to do is to change the xml file. I will refactor my site creation script as an example; you can download the script here and refactored code here.

1. As you can see below, I hard code all the variables in the script itself.


$url = "http://ybbest"
$WebsiteName = "Ybbest"
$WebsiteDesc = "Ybbest test site"
$Template = "STS#0"
$PrimaryLogin = "contoso\administrator"
$PrimaryDisplay = "administrator"
$PrimaryEmail = "administrator@contoso.com"
$MembersGroup = "$WebsiteName Members"
$ViewersGroup = "$WebsiteName Viewers"

2. Next, I will show you how to manipulate xml file using PowerShell. You can use the get-content to grab the content of the file.

[xml] $xmlconfigurations=get-content .\SiteCollection.xml

3. Then you can set it to variable (the variable has to be typed [xml] after that you can read the content of the xml content, PowerShell also give you nice IntelliSense by press the Tab key.

[xml] $xmlconfigurations=get-content .\SiteCollection.xml
$xmlconfigurations.SiteCollection
$xmlconfigurations.SiteCollection.SiteName

4. After refactoring my code, I can set the variables using the xml file as below.


#Set the parameters
$siteInformation=$xmlinput.SiteCollection
$url = $siteInformation.URL
$siteName = $siteInformation.SiteName
$siteDesc = $siteInformation.SiteDescription
$Template = $siteInformation.SiteTemplate
$PrimaryLogin = $siteInformation.PrimaryLogin
$PrimaryDisplay = $siteInformation.PrimaryDisplayName
$PrimaryEmail = $siteInformation.PrimaryLoginEmail
$MembersGroup = "$WebsiteName Members"
$ViewersGroup = "$WebsiteName Viewers"

How to write PowerShell code part 1
How to write PowerShell code part 2
How to write PowerShell code part 3
How to write PowerShell code part 4

Advertisements
  1. woodster
    April 28, 2013 at 2:02 am

    There appears to be some critical pieces missing. Your first step includes hard coded variables in the script: What script? Do you save those hard code variables as an XML? In other words, how did the original XML get created? It appears as if you want to save the hard coded variables into an XML file but that step is missing.
    If I save the hard coded variables “as is” as SiteConfiguration.xml and then try to read it as an XML, I get an error.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: