Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pass parameters ByRef to a function

Status
Not open for further replies.

mrmovie

Technical User
Oct 2, 2002
3,094
GB
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

 
oh my giddy aunt, this appears to be working

Function GetXMLFile([string]$strXMLFile="", [REF]$xmlDoc, [REF]$xmlRoot){
#Function GetXMLFile($strXMLFile, $xmlDoc, $xmlRoot){
write-host "GetXMLFile: strXMLFile = $strXMLFile"
$xmlDoc.Value = New-Object System.Xml.XmlDocument
$xmlDoc.Value.Load($strXMLFile)
$xmlRoot.Value = $xmlDoc.Value.DocumentElement
write-host "GetXmlFile:data.viserver = " + $xmlRoot.Value.data.viserver
}


$strXMLFile = [string]"c:\tools\combined_sccmid_update.xml"
$xmlDoc = [System.Xml.XmlDocument]$null
$xmlRoot = [System.Xml.XmlElement]$null
GetXMLFile $strXMLFile ([REF]$xmlDoc) ([REF]$xmlRoot)
write-host "innerxml " + $xmlRoot.InnerXml
write-host "data.viserver = " + $xmlRoot.data.viserver
 
ok, i am slowly coming round to advantages in powershell (over my real love), mainly today due to

$dicVMHosts = @{}
#
ForEach blaa blaa
$clsVMHost = "" | Select VMWareUUID, VMWareName, VMWareNetbiosname, VMWareSCCMID, VMWareMAC, SMSSCCMID
...
$dicVMHosts.Value.Add($clsVMHost.VMWareUUID, $clsVMHost)
}#Next
$dicVMHosts | Export-CliXml $strXMLFile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top