Friday, August 29, 2014

Update Timer Job Config through PowerShell

Hi,

There was a requirement of updating our timer job config with the below values in app settings section.
Post that we are picking the values from here into our custom timer job.

    <add key="NucleusSiteURL" value="https://oabc.com/sites/Nucleus" />
    <add key="NuclesuSalesTools" value="Sales%20WorkShop" />
    <add key="NucleusPathOfLearnFolder" value="SalesWorkShop/Learn/Verticals/" />
    <add key="NucleusPathOfPresentFolder" value="SalesWorkShop/Present/Verticals/" />
    <add key="NucleusPathOfSendFolder" value="SalesWorkShop/Send/Verticals/" />
    <add key="NucleusNumberOfDays" value="0" />

Now for these entries we had created the below PowerShell script

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Write-Host "Entered???"
$WebAppConfigPath = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\OWSTIMER.EXE.CONFIG"
Write-Host "Loading Web.Config as XML in " $WebAppConfigPath -ForegroundColor Cyan
[xml]$xml = Get-Content $WebAppConfigPath
Write-Host "Started" -ForegroundColor Green
$appSettingNode =$xml.SelectSingleNode("//configuration/appSettings")
if ($appSettingNode -eq $null)
    {
    Write-Host "Inside if loop" -ForegroundColor Green
                $addAppsettingNode=$xml.CreateNode('element',"appSettings","")
    $appSettingNode=$xml.SelectSingleNode("//configuration").AppendChild($addAppsettingNode)
    #$addAppsettingNode=$xml.SelectSingleNode("//configuration").AppendChild($addAppsetting)
    }
    else
    {
     Write-Host "Inside else loop" -ForegroundColor Green
    #$appSettingNode=$xml.SelectSingleNode("//configuration/appSettings").AppendChild($addAppsetting)
    }

$config1=$xml.CreateNode('element',"add","")
$config1.SetAttribute('key','NucleusSiteURL')
$config1.SetAttribute('value','https://ophqa.mindtree.com/sites/Nucleus')
$config2=$xml.CreateNode('element',"add","")
$config2.SetAttribute('key','NuclesuSalesTools')
$config2.SetAttribute('value','Sales%20WorkShop')
$config3=$xml.CreateNode('element',"add","")
$config3.SetAttribute('key','NucleusPathOfMainFolder')
$config3.SetAttribute('value','SalesWorkShop/Learn/Verticals')
$config4=$xml.CreateNode('element',"add","")
$config4.SetAttribute('key','NucleusNumberOfDays')
$config4.SetAttribute('value','0')
$config5=$xml.CreateNode('element',"add","")
$config5.SetAttribute('key','NucleusEmailConfigurationList')
$config5.SetAttribute('value','EmailConfigurationList')
$config6=$xml.CreateNode('element',"add","")
$config6.SetAttribute('key','NucleusNumberOfMinutes')
$config6.SetAttribute('value','5')
$appSettingNode.AppendChild($config1)
$appSettingNode.AppendChild($config2)
$appSettingNode.AppendChild($config3)
$appSettingNode.AppendChild($config4)
$appSettingNode.AppendChild($config5)
$appSettingNode.AppendChild($config6)
$xml.Save($WebAppConfigPath)
Write-Host "Completed" -ForegroundColor Green

Finally it is inserting the values as below

No comments:

Post a Comment