Solved

Need help Manipulating powershell output

Posted on 2009-05-18
7
1,049 Views
Last Modified: 2013-11-25
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
Comment
Question by:dmc2084
  • 5
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
peter_field earned 500 total points
ID: 24419744
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
 

Author Comment

by:dmc2084
ID: 24425561
$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
 

Author Comment

by:dmc2084
ID: 24426141
I also run into New-Object : Cannot find type [PSCustomObject]: make sure the assembly containing this type is loaded.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 4

Expert Comment

by:peter_field
ID: 24427684
Ahh, sorry, I wrote this in PowerShell v2.. change PSCustomObject to PSObject and it should run.
0
 

Author Comment

by:dmc2084
ID: 24432649
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
 

Author Comment

by:dmc2084
ID: 24435761
What would be the best way to output to a file comma separated from $results | ft Hostname, Target, NodeName, PortName, WWULN?
0
 

Author Comment

by:dmc2084
ID: 24435997
Figured it out

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question