- Take back up of Existing Web Config
- Check if appSettings node is there then throw exception
- If property node is then delete it and recreate with new node.
Add-PSSnapin Microsoft.SharePoint.PowerShell
$serverInfo = get-spserver | ? { $_.Role -eq "Application" }
$serverInfo.Name
#$Folderpath = '\C$\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\WebServices\SecurityToken';
$Folderpath ='\C$\inetpub\wwwroot\wss\VirtualDirectories\'
$webappName = @("abc443\")
$PropertyName = @("HostName","PortNumber)
$PropertyValue = @("abc","636)
for($j=0 ; $j -lt $serverInfo.count ; $j++)
{
write-host*****************************" -foreground Green
write-host " Server : " + $serverInfo[$j].Name -foreground Green
write-host "*************************************************" -foreground Green
for($i=0 ; $i -lt $webappName.count ; $i++)
{
$path = '\\'+$serverInfo[$j].Name + $Folderpath
write-host $path
$webConfig = $path+$webappName[$i]+'Web.config'
write-host "Updatng Web Config for " + $webConfig
if(Test-Path $webConfig -PathType Any)
{
$backup = $path+$webappName[$i]+ 'web_'+ (get-date).tostring("yyyy_MM_dd_hh_mm_ss")+'.bak';
write-host "Taking Back up for Web Config " + $backup -foreground Green
copy-item $webConfig -destination $backup
for($k=0 ; $k -lt $PropertyName.count ; $k++)
{
Set-ApplicationSettingConfigProperty $webConfig $PropertyName[$k] $PropertyValue[$k] ;
Function Set-ApplicationSettingConfigProperty
{
param (
[parameter(Mandatory = $true)][ValidateScript ({Test-Path $_})][string] $PathToConfigFile,
[parameter(Mandatory = $true)][string] $PropertyName,
[parameter(Mandatory = $true)][string] $PropertyValue,
[Parameter(Mandatory = $false)][Validatescript ({(Get-Service $_) -ne $null})][string] $NameOfServiceToRestart = $null)
$configurationAppSettingXmlPath = "//configuration/appSettings"
[xml] $configurationDocument = Get-Content $PathToConfigFile
$appSettingsNode = $configurationDocument.SelectSingleNode($configurationAppSettingXmlPath)
if($appSettingsNode -eq $null)
{
# $(throw "App Settings Does not Exists! Invalid Configuration File.")
write-host "App Settings Does not Exists! Invalid Configuration File." -foreground red
}
$nodeToUpdate = $configurationDocument.SelectSingleNode($configurationAppSettingXmlPath+"/add[@key='$PropertyName']")
if($nodeToUpdate -ne $null)
{
Write-Host "[$PropertyName] Already exists, Removing it to re-set its value." -foreground red
$removedElement = $appSettingsNode.RemoveChild($nodeToUpdate)
}
Write-Host "Creating new Configuration Node." -foreground Green
$newPropertyNode = $configurationDocument.CreateNode("element", "add","")
$newPropertyNode.SetAttribute("key", $PropertyName)
$newPropertyNode.SetAttribute("value", $PropertyValue)
$appSettingsNode = $configurationDocument.SelectSingleNode($configurationAppSettingXmlPath).AppendChild($newPropertyNode)
Write-Host "Adding new property into the configuration file." -foreground Green
$configurationDocument.Save($PathToConfigFile)
Write-Host "Property was Successfully Updated." -foreground Yellow
Write-Host "---------------------------------------------------------------------------" -foreground Yellow
Write-Host "---------------------------------------------------------------------------" -foreground Yellow
if([string]::IsNullOrWhiteSpace($NameOfServiceToRestart) -eq $false)
{
Write-Host "Service [$NameOfServiceToRestart] was defined.., Restarting it"
Restart-Service -Name $NameOfServiceToRestart
Write-Host "Service was Restarted..."
}
}
}
}
}
}
No comments:
Post a Comment