I am trying to edit an attribute value in a XML file with the id of the user logged in
i tried :
$user = $env:USERNAME
$xml = [xml](Get-Content "D:\Python\prm.xml")
$node = $xml.BootStrap.DataBaseAliases.LastLoginUserName
$node.SetAttribute("LastLoginUserName", "$user");
It returns an error : You cannot call a method on a null-valued expression. At D:\Python\a.ps1:5 char:1 + $node.SetAttribute("LastLoginUserName", "$user"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Also tried following:
$user = $env:USERNAME
$FileLocation = "D:\Python\prm.xml"
$File = Get-Content $FileLocation
$XML = [XML]$File
$XPpath = "/BootStrap/DataBaseAliases/LastLoginUserName"
$node = $XML.SelectNodes($XPpath)
$node | % { $_.SetAttribute("attribute-1", "$user") }
$XML.OuterXml | Out-File $FileLocation
This doesnt return any error, but xml is unchanged. XML content below:
<?xml version="1.0" encoding="utf-8"?><BootStrap MajorVersion="18"
MinorVersion="8" PatchVersion="5" DeploymentVersion="0"><DataBaseAliases
DefaultPMAlias="Corp_2016" LastLoginUserName="FOOUSER"><Alias>
<Name>PMDB</Name><AppType>Project Management</AppType><Connection>
<DriverName>SQLServer</DriverName><BlobSize>-
i would like to replace FOOUSER with user currently logged in.