Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1066
  • Last Modified:

Need help Manipulating powershell output

I am using powershell to connect to a list of remote hosts and then retrieving the lun information via vender utility and then sending it to a text  
file on the local machine I am running powershell from.

The text file will have all of the Target and lun information for all of the hosts that the script was run against.
The output looks like the following




 
Host        : Host1
IP          : 10.10.10.10
DnsCacheHit : True
Success     : True
ExecTime    : 00
Message     : ------------------------------------------------------------------------------
              Host Name                  : host1
              OS Type                    : Microsoft
              OS Version                 : Service Pack
              SDM API Version            : 44
              ------------------------------------------------------------------------------
              HBA Model 22:
                Port 1 WWPN 22-22-22-22-22-22-22-22 SN 4564564564566 (HBA Instance 0) Online
              HBA Model 22:
                Port 1 WWPN 33-33-33-33-33-33-33-33 SN 4564564564566 (HBA Instance 1) Online
              ------------------------------------------------------------------------------
              Total  HBA(s) : 2
             
              Time and date:            Tue march 20 11:37:32 2040
             
              ------------------------------------------------------------------------------





This output lists targets. If a target has luns they will be shown right after the target is displayed. See how target 7 has no luns but target 8 and  9 do.





------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 22-22-22-22-22-22-22-22 Port ID 22-22-22
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 7
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-11-11-11-11-11-11
              Port Name                  : 22-22-22-22-22-22-22-22
              Port ID                    : 11-11-11
              Product Type               : Device
              Number of LUN(s)           : 0
              Status                     : Online
              ------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 33-33-33-33-33-33-33-33 Port ID 33-3A-33
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 8
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-22-22-22-22-11-1A
              Port Name                  : 11-44-44-44-44-44-44-4A
              Port ID                    : 33-43-33
              Product Type               : Disk
              Number of LUN(s)           : 2
              Status                     : Online
              ------------------------------------------------------------------------------
              --------------------------------------
              LUN 0
              --------------------------------------
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              LUN                        : 0
              Size                       : 1 MB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 11-11-11-22-22-22-22-22
               
              --------------------------------------
              LUN 1
              --------------------------------------
              Product Vendor             : 1122
              Product ID                 : VV              
              Product Revision           : 1122
              LUN                        : 1
              Size                       : 1 GB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 22-22-33-22-44-55-66-55
               
              --------------------------------------
------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 22-22-22-22-22-22-22-22 Port ID 44-44-44
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 9
              Device ID                  : 44
              Product Vendor             : 44
              Product ID                 : VV              
              Product Revision           : 44
              Serial Number              : 4455
              Node Name                  : 44-44-44-44-44-44-44-44
              Port Name                  : 55-55-55-55-55-55-55-55
              Port ID                    : 11-11-11
              Product Type               : Disk
              Number of LUN(s)           : 1
              Status                     : Online
              ------------------------------------------------------------------------------
              --------------------------------------
              LUN 0
              --------------------------------------
              Product Vendor             : 4455
              Product ID                 : 44            
              Product Revision           : 4444
              LUN                        : 0
              Size                       : 1 MB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 77-77-77-77-77-77-77-77
               
              --------------------------------------


The Target and lun list will continue until the next host is listed.  and then repeat for the next host what is listed above or more if it has more luns/targets



Host        : Host2
IP          : 11.11.11.11
DnsCacheHit : True
Success     : True
ExecTime    : 00
Message     : ------------------------------------------------------------------------------
              Host Name                  : host2
              OS Type                    : Microsoft
              OS Version                 : Service Pack
              SDM API Version            : 00
              ------------------------------------------------------------------------------
              HBA Model 22:
                Port 1 WWPN 22-22-22-22-22-22-22-22 SN 4564564566456 (HBA Instance 0) Online
              HBA Model 22:
                Port 1 WWPN 33-33-33-33-33-33-33-33 SN 4564564564645 (HBA Instance 1) Online
              ------------------------------------------------------------------------------
              Total  HBA(s) : 2
             
              Time and date:            Tue march 20 11:37:32 2040
             
              ------------------------------------------------------------------------------



There will be additional host information listed then Lun and target info





My end goal is to pull only host,target,lun,wwuln,node name,port name info and put it into another text file or just only get that info to begin with.

The final output will be in a table looking like


Hostname  Target    Node name                                  port name                    lun         wwuln
 host1          8       00-11-22-33-44-55-66-77     22-33-44-55-66-77-88-99     0       11-11-11-11
 host1          8       00-11-22-33-44-55-66-77     22-33-44-55-66-77-88-99     1       22-22-22-22
 host1          9       00-11-22-33-44-55-66-77     22-33-44-55-66-77-88-99     2     33-33-33-33-33
 host2          1       00-11-22-33-44-55-66-77     22-33-44-55-66-77-88-99     1     44-44-44-44-44


Is this possible? Anyone know a way?
0
dmc2084
Asked:
dmc2084
  • 5
  • 2
1 Solution
 
peter_fieldCommented:
Nasty. Here is what I have come up with.
# The output of the command, for this you would change it to $commandoutput = command /params
$commandoutput = @'
Host        : Host1
IP          : 10.10.10.10
DnsCacheHit : True
Success     : True
ExecTime    : 00
Message     : ------------------------------------------------------------------------------
              Host Name                  : host1
              OS Type                    : Microsoft
              OS Version                 : Service Pack
              SDM API Version            : 44
              ------------------------------------------------------------------------------
              HBA Model 22:
                Port 1 WWPN 22-22-22-22-22-22-22-22 SN 4564564564566 (HBA Instance 0) Online
              HBA Model 22:
                Port 1 WWPN 33-33-33-33-33-33-33-33 SN 4564564564566 (HBA Instance 1) Online
              ------------------------------------------------------------------------------
              Total  HBA(s) : 2
             
              Time and date:            Tue march 20 11:37:32 2040
             
              ------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 22-22-22-22-22-22-22-22 Port ID 22-22-22
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 7
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-11-11-11-11-11-11
              Port Name                  : 22-22-22-22-22-22-22-22
              Port ID                    : 11-11-11
              Product Type               : Device
              Number of LUN(s)           : 0
              Status                     : Online
              ------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 33-33-33-33-33-33-33-33 Port ID 33-3A-33
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 8
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-22-22-22-22-11-1A
              Port Name                  : 11-44-44-44-44-44-44-4A
              Port ID                    : 33-43-33
              Product Type               : Disk
              Number of LUN(s)           : 2
              Status                     : Online
              ------------------------------------------------------------------------------
              --------------------------------------
              LUN 0
              --------------------------------------
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              LUN                        : 0
              Size                       : 1 MB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 11-11-11-22-22-22-22-22
               
              --------------------------------------
              LUN 1
              --------------------------------------
              Product Vendor             : 1122
              Product ID                 : VV              
              Product Revision           : 1122
              LUN                        : 1
              Size                       : 1 GB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 22-22-33-22-44-55-66-55
               
              --------------------------------------
------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 22-22-22-22-22-22-22-22 Port ID 44-44-44
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 9
              Device ID                  : 44
              Product Vendor             : 44
              Product ID                 : VV              
              Product Revision           : 44
              Serial Number              : 4455
              Node Name                  : 44-44-44-44-44-44-44-44
              Port Name                  : 55-55-55-55-55-55-55-55
              Port ID                    : 11-11-11
              Product Type               : Disk
              Number of LUN(s)           : 1
              Status                     : Online
              ------------------------------------------------------------------------------
              --------------------------------------
              LUN 0
              --------------------------------------
              Product Vendor             : 4455
              Product ID                 : 44            
              Product Revision           : 4444
              LUN                        : 0
              Size                       : 1 MB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 77-77-77-77-77-77-77-77
               
              --------------------------------------
Host        : Host2
IP          : 11.11.11.11
DnsCacheHit : True
Success     : True
ExecTime    : 00
Message     : ------------------------------------------------------------------------------
              Host Name                  : host2
              OS Type                    : Microsoft
              OS Version                 : Service Pack
              SDM API Version            : 00
              ------------------------------------------------------------------------------
              HBA Model 22:
                Port 1 WWPN 22-22-22-22-22-22-22-22 SN 4564564566456 (HBA Instance 0) Online
              HBA Model 22:
                Port 1 WWPN 33-33-33-33-33-33-33-33 SN 4564564564645 (HBA Instance 1) Online
              ------------------------------------------------------------------------------
              Total  HBA(s) : 2
             
              Time and date:            Tue march 20 11:37:32 2040
             
              ------------------------------------------------------------------------------              
              ------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 22-22-22-22-22-22-22-22 Port ID 22-22-22
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 7
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-11-11-11-11-11-11
              Port Name                  : 22-22-22-22-22-22-22-22
              Port ID                    : 11-11-11
              Product Type               : Device
              Number of LUN(s)           : 0
              Status                     : Online
              ------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
              HBA Instance 0: QLE2460 Port 1 WWPN 33-33-33-33-33-33-33-33 Port ID 33-3A-33
              ------------------------------------------------------------------------------
              Path                       : 0
              Target                     : 8
              Device ID                  : 1122
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              Serial Number              : 1122
              Node Name                  : 11-11-22-22-22-22-11-1A
              Port Name                  : 11-44-44-44-44-44-44-4A
              Port ID                    : 33-43-33
              Product Type               : Disk
              Number of LUN(s)           : 2
              Status                     : Online
              ------------------------------------------------------------------------------
              --------------------------------------
              LUN 0
              --------------------------------------
              Product Vendor             : 1122
              Product ID                 : 44              
              Product Revision           : 1122
              LUN                        : 0
              Size                       : 1 MB
              Type                       : SBC-2 Direct access block device (e.g., magnetic disk)
              WWULN                      : 11-11-11-22-22-22-22-22
'@
 
 
$results = @()
$currentlun = $null
foreach ( $line in $commandoutput.Split("`n") )
{
    if ( $line -match 'Host Name +?: (?<host>.*)$' )
    {
        if ( $currentlun -ne $null )
        {
            $results += $currentlun
        }
        $currentlun = New-Object PSCustomObject
        $currentlun | Add-Member -MemberType NoteProperty -Name Hostname -Value $matches.host.ToString().Trim()
    }
 
    if ( $line -match 'Target +?: (?<target>.*)$' )
    {
        if ( $currentlun.Target -ne $null )
        {
            $currenthost = $currentlun.Hostname
            $results += $currentlun
            $currentlun = New-Object PSCustomObject
            $currentlun | Add-Member -MemberType NoteProperty -Name Hostname -Value $currenthost
        }
        $currentlun | Add-Member -MemberType NoteProperty -Name Target -Value $matches.target.ToString().Trim()
    }
    
    if ( $line -match 'Node Name +?: (?<nodename>.*)$' )
    {
        $currentlun | Add-Member -MemberType NoteProperty -Name NodeName -Value $matches.nodename.ToString().Trim()
    }
 
    if ( $line -match 'Port Name +?: (?<portname>.*)$' )
    {
        $currentlun | Add-Member -MemberType NoteProperty -Name PortName -Value $matches.portname.ToString().Trim()
    }
 
    if ( $line -match 'WWULN +?: (?<wwuln>.*)$' )
    {
        if ( $currentlun.WWULN -ne $null )
        {
            $currenthost = $currentlun
            $results += $currentlun
            $currentlun = New-Object PSCustomObject
            $currentlun | Add-Member -MemberType NoteProperty -Name Hostname -Value $currenthost.Hostname
            $currentlun | Add-Member -MemberType NoteProperty -Name Target -Value $currenthost.Target
            $currentlun | Add-Member -MemberType NoteProperty -Name NodeName -Value $currenthost.NodeName
            $currentlun | Add-Member -MemberType NoteProperty -Name PortName -Value $currenthost.PortName
        }
        $currentlun | Add-Member -MemberType NoteProperty -Name WWULN -Value $matches.wwuln.ToString().Trim()
    }
}
if ( $currentlun -ne $null )
{
    $results += $currentlun
}
 
$results | ft Hostname, Target, NodeName, PortName, WWULN

Open in new window

0
 
dmc2084Author Commented:
$commandoutput  =  get-content c:\test.txt  

Trying    get-content c:\test.txt  as my command and getting

Method invocation failed because [System.Object[]] doesn't contain a method named 'Split'.At c:\test.txt
0
 
dmc2084Author Commented:
I also run into New-Object : Cannot find type [PSCustomObject]: make sure the assembly containing this type is loaded.
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
peter_fieldCommented:
Ahh, sorry, I wrote this in PowerShell v2.. change PSCustomObject to PSObject and it should run.
0
 
dmc2084Author Commented:
Made the change to PSObject and its working good so far, doing a little more testing.
Thank you so very much. I will replay today with the results.
0
 
dmc2084Author Commented:
What would be the best way to output to a file comma separated from $results | ft Hostname, Target, NodeName, PortName, WWULN?
0
 
dmc2084Author Commented:
Figured it out

did $results | select-object hostname,target | export-csv
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now