Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pass parameters ByRef to a function

pass parameters ByRef to a function

pass parameters ByRef to a function

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
$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


RE: pass parameters ByRef to a function

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
$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

RE: pass parameters ByRef to a function

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)
$dicVMHosts | Export-CliXml $strXMLFile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close