Microsoft Virtualization Discussions

Any way to get lun serial number in hex through powershell?

MACKOPES1
5,194 Views

Get-NaLunSerialNumber doesn't seem to have a way to return the hex value for the lun serial number like 'lun serial -x' does from the command line.

Does anyone know of another 'api' way to get this? I'm trying to use it to cross reference luns with the naa id on a VMware environment (which matches the hex serial number perfectly)

Thanks!

Aaron

1 ACCEPTED SOLUTION

timothyn
5,090 Views

There are a few ways to do it.  First, you could just parse the output of the "lun serial -x" command with a regex like this:

PS C:\> (Invoke-NaSsh "lun serial -x /vol/testluns/lun1" ) -replace ".*(0x\w*).*", "`$1"

0x50344461685a634562587732

But not everybody is nuts about regexes, and the Invoke-NaSsh cmdlet doesn't work if you are using RPC.  So the second method makes use of the fact that the "-x" hex representation is just the characters converted to ASCII hex.  You can do the whole conversion in one line:

PS C:\> ((Get-NaLunSerialNumber /vol/testluns/lun1).ToCharArray() | % {([byte][char]$_).ToString("X2")}) -join ""

50344461685A634562587732

But that's a little unwieldy, especially if you want to reuse it later.  So, I'd probably define a function that does that for you:

function ConvertTo-HexString([string]$input)

{

    [byte[]]$bytes = $input.ToCharArray()

    $hexBytes = $bytes | % {$_.ToString("X2")}

    $hexBytes -join ""

}


PS C:\> Get-NaLunSerialNumber /vol/testluns/lun1 | ConvertTo-HexString

50344461685A634562587732

Then you can put that function in the top of your script.  Or if it's something you want to use from the commandline everyday, you can put it in your PS profile (the path is in the $profile variable) and then restart PowerShell.

View solution in original post

3 REPLIES 3

timothyn
5,091 Views

There are a few ways to do it.  First, you could just parse the output of the "lun serial -x" command with a regex like this:

PS C:\> (Invoke-NaSsh "lun serial -x /vol/testluns/lun1" ) -replace ".*(0x\w*).*", "`$1"

0x50344461685a634562587732

But not everybody is nuts about regexes, and the Invoke-NaSsh cmdlet doesn't work if you are using RPC.  So the second method makes use of the fact that the "-x" hex representation is just the characters converted to ASCII hex.  You can do the whole conversion in one line:

PS C:\> ((Get-NaLunSerialNumber /vol/testluns/lun1).ToCharArray() | % {([byte][char]$_).ToString("X2")}) -join ""

50344461685A634562587732

But that's a little unwieldy, especially if you want to reuse it later.  So, I'd probably define a function that does that for you:

function ConvertTo-HexString([string]$input)

{

    [byte[]]$bytes = $input.ToCharArray()

    $hexBytes = $bytes | % {$_.ToString("X2")}

    $hexBytes -join ""

}


PS C:\> Get-NaLunSerialNumber /vol/testluns/lun1 | ConvertTo-HexString

50344461685A634562587732

Then you can put that function in the top of your script.  Or if it's something you want to use from the commandline everyday, you can put it in your PS profile (the path is in the $profile variable) and then restart PowerShell.

MACKOPES1
5,090 Views

Oh perfect! Thanks much Eric!

I had just figured out the route with invoke-nassh but like you said, then I can't use RPC and integrated auth so I was going down the road of prompting for credentials.

This is much better! Thanks again!

AK

MACKOPES1
5,090 Views

FYI, I tweaked it a bit more to put it in a single function:

###############################################################################################

## Get-NALunSerialNumberHex: Function that gathers the Hex Serial number of an NALun

###############################################################################################

Function Get-NALunSerialNumberHex {

    param (

        [parameter(Mandatory=$true)]

        $LunPath

    )

    Write-Host "[Gathering Lun Serial Number]" -ForegroundColor Cyan

    $LunSerial = ((Get-NaLunSerialNumber $LunPath).ToCharArray() | % {([byte][char]$_).ToString("X2")}) -join ""

    return $LunSerial

}

Public