Link to home
Start Free TrialLog in
Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on

Edit multiple Config Files

I have multiple webconfig files that I need to edit.
Is there a smart way to do this with scripting?

I have a csv file with two columns (User and NewPw) and I need to edit this line which have different password and userName in each WebConfig file
<identity impersonate="true" password="kjsjhdsj-dfsf343-3432efd5-ijt548" userName="DOMAIN\User1" />

Open in new window


Any good ideas on how to do this through Powershell? RegEx maybe?
Avatar of Dorababu M
Dorababu M
Flag of India image

May be you can write a module as follows and call that for each config file. The following I used to update the database instance with the required one. Try changing this as per your need

function ModifyConnectionString
{
    [CmdletBinding()]
    param (
        [String] $XmlaFile,
        [String] $SqlServerInstance = "(local)"
    )

    begin 
    { 
        ## Preserve caller preferences.  Used to allow -Verbose flag to carry throughout execution.
        Try { Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState } Catch { <# Do Nothing #> }
        Write-Verbose "Begin function:  '$($MyInvocation.MyCommand)' ..."
    }

    process
    {
        $ModifySettings = [xml](Get-Content $XmlaFile)
        $CubeName = $ModifySettings.Batch.Alter.Object.DatabaseID

        Write-Output "Modifying the connectionstring dynamically"

        #looping through all the child nodes that matches the node connectionstring and modifying the connectionstring

        $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ChildNodes | Where-Object Name -Match 'ConnectionString' | ForEach-Object {
        
            $DataBase = $_.'#text'.Split(";").Split("=")
            $DataBase = $DataBase[$DataBase.Length - 1]

            $ConnectionString = "Provider=SQLNCLI11.1;Data Source=$SqlServerInstance;Integrated Security=SSPI;Initial Catalog =$Database"

            $_.'#text' = "$ConnectionString"

        }

        Write-Output "Changed the connectionstring"

        Write-Output "`n Modifying the ImpersonationMode and Account to defaults"

        $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.ImpersonationMode = "Default"

        if($ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.Account -ne $null)
        {
		    $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ImpersonationInfo.Account = "Administrator"
        }

        Write-Output "`n saving the modifications"
        $ModifySettings.Batch.Alter.ObjectDefinition.Database.DataSources.DataSource.ConnectionString
        $ModifySettings.Save($XmlaFile)
        Write-Output "`n saved the modifications"
    }

    end 
    { 
        Write-Verbose "End function:  '$($MyInvocation.MyCommand)'"
    }
    
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dorababu M
Dorababu M
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial