I have been pulling my hair out over this and i am ready to burn my powershell books.
I simply want to pass a function a couple of variables ByRef. So, when the function has finished i have some System.Xml objects to use in the rest of the script. I have tried various variations, [ref] declared at the function call, with or without declaring my variable types before passing to function, with or without $null when declaring variables and at the function declaration. iver tried using new variables names in the function declaration (other than reusing the names for clarity) this has no effect
Function GetXMLFile([string]$strXMLFile="", [System.Xml.XmlDocument]$xmlDoc, [System.Xml.XmlElement]$xmlRoot){
write-host "GetXMLFile: strXMLFile = $strXMLFile"
$xmlDoc = New-Object System.Xml.XmlDocument
$xmlDoc.Load($strXMLFile)
$xmlRoot = $xmlDoc.DocumentElement
write-host "GetXmlFile:data.viserver = " + $xmlRoot.data.viserver
}
$strXMLFile = [string]"c:\tools\combined_sccmid_update.xml"
$xmlDoc = [System.Xml.XmlDocument]$null
$xmlRoot = [System.Xml.XmlElement]$null
GetXMLFile($strXMLFile, $xmlDoc, $xmlRoot)
write-host "innerxml " + $xmlRoot.InnerXml
write-host "data.viserver = " + $xmlRoot.data.viserver
I get the "GetXmlFile:data.viserver = " and i get to see what the XmlNode text contains. however, the subsequent write-host "data.viserver = " + $xmlRoot.data.viserver
is blank.
Ive seen a few posts on the [ref] keyboard but trying this just leads to other runtimes and a sucky use of xmlDov.Value.Load./..and still not contents passed back ByRef
I simply want to pass a function a couple of variables ByRef. So, when the function has finished i have some System.Xml objects to use in the rest of the script. I have tried various variations, [ref] declared at the function call, with or without declaring my variable types before passing to function, with or without $null when declaring variables and at the function declaration. iver tried using new variables names in the function declaration (other than reusing the names for clarity) this has no effect
Function GetXMLFile([string]$strXMLFile="", [System.Xml.XmlDocument]$xmlDoc, [System.Xml.XmlElement]$xmlRoot){
write-host "GetXMLFile: strXMLFile = $strXMLFile"
$xmlDoc = New-Object System.Xml.XmlDocument
$xmlDoc.Load($strXMLFile)
$xmlRoot = $xmlDoc.DocumentElement
write-host "GetXmlFile:data.viserver = " + $xmlRoot.data.viserver
}
$strXMLFile = [string]"c:\tools\combined_sccmid_update.xml"
$xmlDoc = [System.Xml.XmlDocument]$null
$xmlRoot = [System.Xml.XmlElement]$null
GetXMLFile($strXMLFile, $xmlDoc, $xmlRoot)
write-host "innerxml " + $xmlRoot.InnerXml
write-host "data.viserver = " + $xmlRoot.data.viserver
I get the "GetXmlFile:data.viserver = " and i get to see what the XmlNode text contains. however, the subsequent write-host "data.viserver = " + $xmlRoot.data.viserver
is blank.
Ive seen a few posts on the [ref] keyboard but trying this just leads to other runtimes and a sucky use of xmlDov.Value.Load./..and still not contents passed back ByRef