How to write PowerShell code part 4 (using loop)
- by ybbest
In this post, I’d like to show you how to loop through the xml element. I will use the list data deletion script as an example. You can download the script here.
1. To perform the loop, I use foreach in powershell. Here is my xml looks like
<?xml version="1.0" encoding="utf-8"?>
<Site Url="http://workflowuat/npdmoc">
<Lists>
<List Name="YBBEST Collaboration Areas" Type="Document Library"/>
<List Name="YBBEST Project" />
<List Name="YBBEST Document"/>
</Lists>
</Site>
2. Here is the PowerShell to manipulate the xml. Note, you need to get to the $configurationXml.Site.Lists.List variable rather than $configurationXml.Site.Lists
foreach ($list in $configurationXml.Site.Lists.List){
AppendLog "Clearing data for $($list.Name) at site $weburl" Yellow
if($list.Type -eq "Document Library"){
deleteItemsFromDocumentLibrary -Url $weburl -ListName $list.Name
}else{
deleteItemsFromList -Url $weburl -ListName $list.Name
}
AppendLog "Data in $($list.Name) at $weburl is cleared" Green
}
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