none
Parameter passing to shell from batch

    Question

  • hi

    I am passing the db name & xml file path from the batch file to .ps1 file, to update the xml file with value for database, this is working perfectly till here.

    Now when I tried to add one more parameter to include the server name (may contain ip address), and I am passing this again from the same batch file, now it is not working.

    below is the .pl1 file xml file content 

    param([String]$DatabaseName,[String]$XML,[String]$DatabaseServer)
    [string]$db = 'Catalog='+ $DatabaseName
    [string]$dbs = 'Source='+ $DatabaseServer
    (Get-Content $XML) | 
    Foreach-Object {$_ -replace "Catalog=([A-Za-z0-9_]+)", $db} {$_ -replace "Source=([A-Za-z0-9_]+.)", $dbs}| Set-Content $XML

    --xml file ( here need to change two values 

    --Catalog=Stage --this is working

    --Data Source=localhost -- this is not working with the above code

    <Configuration ConfiguredType="Property" Path="\Package.Connections[ConnStaging].Properties[ConnectionString]" ValueType="String">
    <ConfiguredValue>Data Source=localhost;Initial Catalog=Stage;Integrated Security=SSPI; Connection Timeout = 10</ConfiguredValue>
    </Configuration>


    Neil

    Friday, March 13, 2015 7:33 PM

Answers

  • Try this

    param([String]$DatabaseName='ABC',[String]$XML='c:\web.config',[String]$DatabaseServer='SERVERNAME')
    [string]$db = 'Catalog='+ $DatabaseName
    [string]$dbs = 'Source='+ $DatabaseServer
    (Get-Content $XML) | 
    Foreach-Object {
    $_ -replace "Catalog=([A-Za-z0-9_]+)", $db  -replace "Source=([A-Za-z0-9_]+)", $dbs } | Set-Content $XML
    --Prashanth

    • Proposed as answer by jmcmullen Friday, March 13, 2015 8:42 PM
    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 8:05 PM
  • Prashanth's solution works for me against your original file
    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 8:45 PM
  • There can be many variations in how a connection string is deployed.  It is recommended that you use the string build to prevent corrupting the config file which can crash the web server.

    $config = [xml]@'
    <?xml version="1.0"?>
    <configuration>
        <system.web>
                <compilation optimizeCompilations="false" />
                 <Configuration ConfiguredType="Property" Path="\Package.Connections[ConnStaging].Properties[ConnectionString]" ValueType="String">
                      <ConfiguredValue>Data Source=localhost;Initial Catalog=Stage;Integrated Security=SSPI; Connection Timeout = 10</ConfiguredValue>
                 </Configuration> 
        </system.web>
    </configuration>
    '@
    $DatabaseName = 'NorthWind'
    $DatabaseServer = 'Contoso'
    #$config=[xml](Get-Content web.config)
    Try {
        $connString = $config.selectSingleNode('//Configuration/ConfiguredValue').InnerText
        $builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($connString)
        $builder.'Initial Catalog' = $DatabaseName
        $builder.'Data Source' = $DatabaseServer
        $newStr = $builder.ConnectionString
        Write-Host "Changing to:$newStr" -fore green
        $config.selectSingleNode('//Configuration/ConfiguredValue').InnerText = $newStr
        $config.Save("$pwd\web.config")
    } 
    Catch {
        Write-Host "$_" -ForegroundColor red
    }
    
    
    


    This assures that you have the correct cionnection value and it edits it with a "builder".


    ¯\_(ツ)_/¯

    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 10:51 PM

All replies

  • Try this

    param([String]$DatabaseName='ABC',[String]$XML='c:\web.config',[String]$DatabaseServer='SERVERNAME')
    [string]$db = 'Catalog='+ $DatabaseName
    [string]$dbs = 'Source='+ $DatabaseServer
    (Get-Content $XML) | 
    Foreach-Object {
    $_ -replace "Catalog=([A-Za-z0-9_]+)", $db  -replace "Source=([A-Za-z0-9_]+)", $dbs } | Set-Content $XML
    --Prashanth

    • Proposed as answer by jmcmullen Friday, March 13, 2015 8:42 PM
    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 8:05 PM
  • Prashanth's solution works for me against your original file
    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 8:45 PM
  • There can be many variations in how a connection string is deployed.  It is recommended that you use the string build to prevent corrupting the config file which can crash the web server.

    $config = [xml]@'
    <?xml version="1.0"?>
    <configuration>
        <system.web>
                <compilation optimizeCompilations="false" />
                 <Configuration ConfiguredType="Property" Path="\Package.Connections[ConnStaging].Properties[ConnectionString]" ValueType="String">
                      <ConfiguredValue>Data Source=localhost;Initial Catalog=Stage;Integrated Security=SSPI; Connection Timeout = 10</ConfiguredValue>
                 </Configuration> 
        </system.web>
    </configuration>
    '@
    $DatabaseName = 'NorthWind'
    $DatabaseServer = 'Contoso'
    #$config=[xml](Get-Content web.config)
    Try {
        $connString = $config.selectSingleNode('//Configuration/ConfiguredValue').InnerText
        $builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($connString)
        $builder.'Initial Catalog' = $DatabaseName
        $builder.'Data Source' = $DatabaseServer
        $newStr = $builder.ConnectionString
        Write-Host "Changing to:$newStr" -fore green
        $config.selectSingleNode('//Configuration/ConfiguredValue').InnerText = $newStr
        $config.Save("$pwd\web.config")
    } 
    Catch {
        Write-Host "$_" -ForegroundColor red
    }
    
    
    


    This assures that you have the correct cionnection value and it edits it with a "builder".


    ¯\_(ツ)_/¯

    • Marked as answer by Neilcse Monday, March 16, 2015 2:58 PM
    Friday, March 13, 2015 10:51 PM