?
Solved

Need help Manipulating powershell output

Posted on 2009-05-18
7
Medium Priority
?
1,060 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
peter_field earned 2000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 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