Powershell – Working with Cluster Resources – Byte Arrays

Kyle Neier, 2012-03-19

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
{
<#

.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

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis

2009-02-23

1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren

2009-02-17

1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren

2009-02-13

360 reads