I am using WMI to find out what my WWN (World Wide Name) is for my port on an HBA card. I can get the WWN back but it is contained as an 8 byte array. I would like to convert this byte array into a string of 16 hex digits for easy display.

This is the query I am using to print out each number in its own line. Is there a way to convert this to have the 8 lines combined onto a single line?

gwmi -namespace root\wmi -class MSFC_FibrePortNPIVAttributes | select -expand WWPN | foreach { $_.ToString("X2") }

I think the following can be used to test with just the byte data but I'm still new to PowerShell.

[byte[]] 1,2,3,4,5,6,7,8 | foreach { $_.ToString("X2") }
link|improve this question
feedback

1 Answer

up vote 1 down vote accepted

Here are a few ways (I'm sure there are others):

[byte[]](1,2,3,4,5,6,7,8) | foreach { $string = $string + $_.ToString("X2") }
Write-Output $string

or

-join ([byte[]](1,2,3,4,5,6,7,8) |  foreach {$_.ToString("X2") } )

or

([byte[]](1,2,3,4,5,6,7,8) |  foreach { $_.ToString("X2") }) -join ""

Output for each of the above:

0102030405060708
link|improve this answer
This lead me down the correct path. I ended up with the following command that does what I need. gwmi -namespace root\wmi -class MSFC_FibrePortNPIVAttributes | select WWPN | foreach {[array]::Reverse($_.WWPN); [BitConverter]::ToUInt64($_.WWPN, 0).ToString("X") } – Jason Aug 31 '10 at 0:06
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.