2

I'm fairly inexperienced in PoSh, however I think I can hold my own on the noob circuit. I'm trying to work on a mechanism to retrieve direct attached storage (DAS) from a list of servers in a SQL table via PowerShell. The commands to grab the DAS must be run locally from the server it's attached to. Because each server can have several DAS devices attached, I've opted to create an array for each server and populate that array with an object for each DAS unit. I've gotten the process nailed down and am able to iterate through each server and run the commands on each server remotely and capture the data I want (within the remote sessions), however I'm having trouble getting the array of objects back out of the remote sessions so that we can write this data to a database table. If I call the array within the invoke-command scriptblock, all is well and I get the data I need - it's only when I attempt to call it afterwards. I don't plan to establish SQL sessions on each of these servers (hundreds) to write the data, so I'd prefer to do it from the machine I'm running this from.

Because this data will be written directly to a database, I'd also prefer to leverage it natively within PowerShell within the same script, rather than mess with exporting/importing files, csv, etc.

I realize that PowerShell serializes objects on the wire, but my understanding is that it rehydrates it once it's back to the local machine. Is what I'm trying to accomplish possible or am I just losing my mind?

The relevant code is:

    $result = invoke-command -computer $FQDN -credential $mycred -ArgumentList $FQDN -scriptblock {
        param($FQDN)
        $DASArray = @() 
    $OMReport = get-wmiobject -class win32_product | where-object {$_.Name -like 'Dell OpenManage*'}
        if (!$OMReport)
        {
        }
        else
        {

            [xml]$DAS = omreport storage enclosure -fmt xml

            $Nodelist = $DAS.SelectNodes("/OMA/Enclosures/DCStorageObject")
            foreach ($Node in $Nodelist | where-object {($_.SelectSingleNode("EnclosureType").get_InnerXML()) -ne 1})
            {
                $DASValues = New-Object PSObject
                $DASValues | Add-Member –MemberType NoteProperty –Name FQDN –Value $FQDN 
                $DASValues | Add-Member –MemberType NoteProperty –Name ID –Value $Node.selectsinglenode("ObjID").get_InnerXML()
                $DASValues | Add-Member –MemberType NoteProperty –Name Model –Value $Node.selectsinglenode("AssetName").get_InnerXML()
                $DASValues | Add-Member –MemberType NoteProperty –Name EnclosureType –Value $Node.selectsinglenode("EnclosureType").get_InnerXML()
                $DASValues | Add-Member –MemberType NoteProperty –Name ServiceTag –Value $Node.selectsinglenode("ServiceTag").get_InnerXML()

            $DASArray += $DASValues

            }
         }

     } 
$result
if (!$DASArray)
{
}
else
{
$DASArray
}                   
}

Some sample output when called from within the scriptblock:

FQDN               : server1
ID                 : 16777237
Model              : MD1200
EnclosureType      : 7
ServiceTag         : XXXXX1   
PSComputerName     : server1
RunspaceId         : 999231e0-5be1-4b8b-8cf1-6462a9fccad3
PSShowComputerName : True

FQDN               : server1
ID                 : 16777238
Model              : MD1200
EnclosureType      : 7
ServiceTag         : XXXXX2   
PSComputerName     : server1
RunspaceId         : 999231e0-5be1-4b8b-8cf1-6462a9fccad3
PSShowComputerName : True

FQDN               : server1
ID                 : 16777239
Model              : MD1200
EnclosureType      : 7
ServiceTag         : XXXXX3   
PSComputerName     : server1
RunspaceId         : 999231e0-5be1-4b8b-8cf1-6462a9fccad3
PSShowComputerName : True

FQDN               : server2
ID                 : 16777240
Model              : MD1200
EnclosureType      : 7
ServiceTag         : XXXXXX4  
PSComputerName     : server2
RunspaceId         : 999231e0-5be1-4b8b-8cf1-6462a9fccad3
PSShowComputerName : True

Any thoughts?

pgapepper
  • 21
  • 2
  • 2
    Your scriptblock doesn't have any output. Just before the closing `}` (on line 27 of your sample) add a line with just $DASArray so that it outputs the array into $Result. Then you can add $result to your database, or whatever you please. – TheMadTechnician Jul 02 '14 at 23:12
  • @TheMadTechnician is correct, but there is something else I noticed. You are building $DASArray in the script block, which means it is being built on the remote machine. But then you are referencing it *below* the script block. That $DASArray is a completely different variable, and will always be $null. It is true that Powershell will serialize and rehydrate objects over the wire, but only the objects that are output by the scriptblock. Adding $DASArray to the bottom of the scriptblock as TheMadTechnician suggests will accomplish that. – JamesQMurphy Jul 03 '14 at 04:28
  • Wow, just wow. I knew I'd been staring at this for WAY too long to miss such a simple solution. Big thanks TheMadTechnician and JamesQMurphy. I knew it could be done - I was just losing my mind. – pgapepper Jul 03 '14 at 21:42

0 Answers0