Wednesday, February 6, 2013

Get values from configuration file in Power Shell script


When you write a Power Shell script sometimes you may require to get some parameter values from the configuration file. While deployment usually we need to passing some values from the configuration file.

You can create configuration file and take those values from it to the PowerShell script. Example we may need to create sub sites or lists / libraries or content types etc. To do this you need site URL to be passed to the script.

You may also need to pass in list name with site URL post deployment when the system is in use and later you want to make any changes.

We can create XML file which has data that can be passed to the PowerShell.

Let's create one configuration file.



 here we have create one XML file which has top level element as configuration. inside that we have site collection where we have passed one site collection URL.

Let's now create one power shell script and get the value from XML file to the script.


What we have done is we took the xml file reference and then declared one variable in which we will store site URL.

After that we get the value of that specific site collection URL. We have written loop here because you can also define multiple <name> inside <site collection> tag.

As of now we are considering one single site collection.

and then we print the site URL in green color with Write-Host options.

Now open the SharePoint 2010 management shell.


and execute the script by typing ./PowerShellScript.ps1

and here is what you get.
In the same way, you can define as many tags as you like in XML file and can take those values inside the power shell script.

I hope this helps.




No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com