SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Powershell - Working with Cluster Resources - Byte Arrays

Over the next few weeks, I plan to share some of the discoveries I've had when working with Windows 2008R2 Clusters and Powershell. I've had the pleasure of changing disk resource names, adding IP Address and Network Names (CAPs), Implementing DTC, adding File Shares, adding custom resources, and even installing SQL Server 2005 and 2008 - all with Powershell. In this series, I hope to provide the community with some of the hurdles I had to overcome and the solution I came up with.

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

Converts a Byte Array to a string array of the 2 character hex equivalents.

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.

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.

Get-ClusterResource "DiskResourceName" | Get-ClusterParameter DiskVolumeInfo | Convert-ByteArrayToHexString

Pipe an object with a .Value property to the function to get the hex string
representation of the byte array

Convert-ByteArrayToHexString -Value $DiskID[31..28])

Return the hex string array representation of a byte array variable at
specific positions in the array

[string]::join("", (Convert-ByteArrayToHexString -Value $DiskID[35..32+37..36+39..38+40..47]))

Take a byte array from specific positions and return a single string from the value

Author: Kyle Neier
Company: Perpetual Technologies, Inc.
Blog: http://sqldbamusings.blogspot.com
Twitter: Kyle_Neier



byte[]] $Value

$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

Kyle Neier

Kyle has been working with computers professionally since 1995 in both a development and administrative capacity. For over 10 of those years, SQL Server has been the focus of both a professional and personal passion. Kyle has worked in online retail, manufacturing, EMR software, and pharmaceutical industries. Kyle joined PTI in 2011 as a Senior Microsoft Consultant where he continues to chase his passion of automating DBA processes and doing everything humanly possible to prevent the 0200 page. Kyle serves as the Vice-President of the Indianapolis chapter of PASS (www.indypass.org) and served on the committee to bring SQL Saturday to Indianapolis. He shares his passion for SQL Server with others through speaking and mentoring at local and regional events.


Leave a comment on the original post [sqldbamusings.blogspot.com, opens in a new window]

Loading comments...