April 11, 2016 at 1:47 pm
Hello,
I am trying to update a connection string in an XML file using POWERSHELL. I have the following code that is not working. I have a code snippet for the XML file and my PS script below.
Any help would be appreciated! BigGrin
POWERSHELL script
# Read configuration file in a XML variable
$webConfig = 'C:\MyXML.dtsConfig'
$doc = (Get-Content $webConfig) -as [Xml]
$root = $doc.get_DocumentElement();
$newCon = $root.Configuration.add.ConfiguredValue.Replace('Data Source=NYD','Data Source=MILANOMOSH');
$root.Configuation.add.ConfiguredValue = $newCon
$doc.Save($webConfig)
XML File OUTPUT
<?xml version="1.0"?>
<DTSConfiguration>
<DTSConfigurationHeading>
<DTSConfigurationFileInfo GeneratedBy="NA\John.Doe" GeneratedFromPackageName="MP_FEED_MAIN" GeneratedFromPackageID="{E80FF572-65D9-49D3-9B19-B5975A29F663}" GeneratedDate="4/11/2016 2:10:27 PM" />
</DTSConfigurationHeading>
<Configuration ConfiguredType="Property" Path="\Package.Connections[NYD.DATABASE].Properties[ConnectionString]" ValueType="String">
<ConfiguredValue>Data Source=NYD;Initial Catalog=DBNAME;Provider=SQLNCLI11.1;Integrated Security=SSPI;Auto Translate=False;</ConfiguredValue>
</Configuration>
</DTSConfiguration>
ERROR MESSAGE
You cannot call a method on a null-valued expression.
At line:6 char:58
+ $newCon = $root.Configuration.add.ConfiguredValue.Replace <<<< ('Data Source=NYD','Data Source=MANDOMOSH');
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Property 'Replace' cannot be found on this object; make sure it exists and is settable.
At line:7 char:41
+ $root.Configuration.add.ConfiguredValue. <<<< Replace = $newCon
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The are no problems, only solutions. --John Lennon
April 12, 2016 at 5:28 pm
I am sure that the add method needs more than just:
$root.Configuration.add
Unfortunately, I am away from a computer.
Gaz
-- Stop your grinnin' and drop your linen...they're everywhere!!!
April 13, 2016 at 10:22 am
Thanks for your response. I resolved this issue in the following blog...
http://www.sqlservercentral.com/Forums/Topic1776398-1351-1.aspx#bm1776799
The are no problems, only solutions. --John Lennon
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy