Clean Up Your PowerShell Environment by Tracking Variable Use: New guest blog article on “Hey, Scripting Guy!”

Today my second guest blog article on “Hey, Scripting Guy!” was published:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/08/21/clean-up-your-powershell-environment-by-tracking-variable-use.aspx

Thanks for publishing, Scripting Guy!

 

 

 

 

 

Me on “Hey, Scripting Guy” blog!

Let me say this in PowerShell language 🙂

$infinite=
"I'm $(for($i=0;$i -lt $infinite; $i++){'very'}) happy about the possiblity to publish "+`
"a guest article on Microsofts ""Hey, Scripting Guy"" blog today. Thanks to the "+`
"Microsoft Scripting Guy, Ed Wilson, for the invitation!"

Here it is:

Learn About Two CodePlex Projects: PS2EXE and RoboPowerCopy

http://blogs.technet.com/b/heyscriptingguy/archive/2011/07/22/learn-about-two-codeplex-projects-ps2exe-and-robopowercopy.aspx

It’s an article about my both projects “RoboPowerCopy” and “PS2EXE”.

Add web.config Modification with PowerShell (SPWebConfigModification)

Here is a script I used to add some web.config modifications with PowerShell. In this case I want to add a custom authentication provider. – The following script I used for setup purpose.

# Load SharePoint PowerShell PSSnapIn and the main SharePoint .net library
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$sharePointDLL = $ENV:CommonProgramFiles+("Microsoft SharedWeb Server Extensions14ISAPIMicrosoft.SharePoint.dll")
[System.Reflection.Assembly]::LoadFile($sharePointDLL) | out-null

# Show Farm BuildVersion to ensure the SharePoint .net library is loaded
$localFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$localFarm.BuildVersion

# store some settings and objects in variables
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
$farmServices = @($webapp.Farm.Services | where-object { $_.TypeName -eq "Microsoft SharePoint Foundation Web Application" } )[0]
$assembly = "MyAuthenticationProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"

# Remove old web.config modifications of MyAuthenticationProvider
$oldMods = @();
$webapp.WebConfigModifications | where-object { $_.Owner -eq "MyAuthenticationProvider" } | foreach-object { $oldMods = $oldMods + $_}

$oldMods | foreach-object{ $webapp.WebConfigModifications.Remove($_) }

# update the Web Application and apply all existing web.config modifications - this executes the "remove" actions from above
$webapp.Update()
$farmServices.ApplyWebConfigModifications()

# New web.config modifications for MyAuthenticationProvider
$myModification1 = new-object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$myModification1.Path = "configuration/system.web/membership/providers"
$myModification1.Name = "add[@name='MyAuthenticationProvider'][@type='MyAuthenticationProvider.MyMembershipProvider, " + $assembly + "']"
$myModification1.Sequence = 0
$myModification1.Owner = "MyAuthenticationProvider"
$myModification1.Type = 0           #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$myModification1.Value = "<add name='MyAuthenticationProvider' type='MyAuthenticationProvider.MyMembershipProvider, " + $assembly + "' />"
$webapp.WebConfigModifications.Add($myModification1)

$myModification2 = new-object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$myModification2.Path = "configuration/system.web/roleManager/providers"
$myModification2.Name = "add[@name='MyAuthenticationProvider'][@type='MyAuthenticationProvider.MyRoleProvider, " + $assembly + "']"
$myModification2.Sequence = 0
$myModification2.Owner = "MyAuthenticationProvider"
$myModification2.Type = 0           #for the enum value "SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode"
$myModification2.Value = "<add name='MyAuthenticationProvider' type'MyAuthenticationProvider.MyRoleProvider, " + $assembly + "' />"
$webapp.WebConfigModifications.Add($myModification2)

# Update the Web Application and apply all exisiting web.config modifications including the new from above

$webapp.Update()
$farmServices.ApplyWebConfigModifications()

Please notice my disclaimer in the right sidebar!