As always, I welcome any suggestions for improvement and encourage you to share how you may have overcome these same obstacles.
The first issue I had was working with Byte Arrays. There are several cluster parameters that are byte arrays. Learning how to work with these was a small hurdle that I was able to overcome with the development of this function. This function is simple - it does little more than convert a byte array into a string array of hex values, but having the hexadecimal representation of a byte array at my finger tips has come in handy on multiple occasions.
The first opportunity I had to use this function was when obtaining the DiskID out of a cluster disk resource. This function was written to make that operation a snap, but because of its reuse-ability, I've used it (and it's changed names as it became more general) dozens of times. I tried my best to comment and to fully support the get-help cmdlet, but if you have questions, feel free to contact me on Twitter or leave a comment.
Happy PoSH!
function Convert-ByteArrayToHexString { <# .SYNOPSIS Converts a Byte Array to a string array of the 2 character hex equivalents. .DESCRIPTION The specific purpose of this function is to be able to obtain the DiskID from the ClusterParameter "DiskVolumeInfo" object the DiskID, but this evolved to be used with any object with the property of value which contains a byte array. In all cases, it returns a string array, each member of the array representing the two character hex conversion of the byte in the same position as provided. .PARAMETER Value This parameter can be fed from the pipeline or as a commandline parameter. If not sent from Pipeline, the -Value parameter option can still be used. .EXAMPLE Get-ClusterResource "DiskResourceName" | Get-ClusterParameter DiskVolumeInfo | Convert-ByteArrayToHexString Description ----------- Pipe an object with a .Value property to the function to get the hex string representation of the byte array .EXAMPLE Convert-ByteArrayToHexString -Value $DiskID[31..28]) Description ----------- Return the hex string array representation of a byte array variable at specific positions in the array .EXAMPLE [string]::join("", (Convert-ByteArrayToHexString -Value $DiskID[35..32+37..36+39..38+40..47])) Description ----------- Take a byte array from specific positions and return a single string from the value .NOTES Author: Kyle Neier Company: Perpetual Technologies, Inc. Blog: http://sqldbamusings.blogspot.com Twitter: Kyle_Neier #> param ( [parameter( Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [byte[]] $Value ) process { $arrayOfBytes = $Value [array]$arrayOfStrings = @() #enumerate over each of the bytes in the array provided foreach($byte in $arrayOfBytes) { #Add the string representation in Hex format to the array $arrayOfStrings += "{0:X2}" -f $byte } #Output the array we have built which contains all of the hex strings Write-Output $arrayOfStrings } }
About Kyle Neier
Husband of a magnificent woman, father of 5, SQL Server geek, IndyPASS Vice President and Food Guy, DBA automation zealot, amateur Powershell evangelist. Follow Me on Twitter
No comments:
Post a Comment