Solved

PowerShell: Combining arrays

Posted on 2008-10-28
3
2,879 Views
Last Modified: 2012-08-13

I have a little function, it has a daft name, but I never was any good at thinking of those. It accepts an object as input and returns a Byte Array. I use it to combine several different Byte Arrays into a one (the end result being a byte array I can send with the UDPClient).

Does anyone happen to know if there a quicker / simpler way of doing this?

The return value is modified to maintain the value type, without it implicitly converts to an Object type (my thanks go to MoW for that one).

Chris
function BuildCompositeArray ($Object) {
  # Returns Byte Array for referenced object
 
  $i = 0; $ByteArray = New-Object Byte[] ($Object.Count);
  ForEach ($Byte in $Object) {
    $ByteArray[$i] = $Byte; $i++;
  }
  # Stop implicit conversion to Object
  Return @(,$ByteArray);
}

Open in new window

0
Comment
Question by:Chris Dent
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
BSonPosh earned 500 total points
ID: 22821764
You coulda emailed me directly :)

You can simply do this

function foo{
[Byte[]]$ByteArray = @()
foreach($byte in $input){$ByteArray += $byte}
$ByteArray
}

Then pipe the data into the function
32,53,33,51,22 | foo

BTW... below is my WOL script
function SendWOL{
    param ($macaddy)
    $mymac = $macaddy.split(':') | %{ [byte]('0x' + $_) }
    if ($mymac.Length -ne 6)
    {
        throw 'Mac Address Must be 6 hex Numbers Separated by : or -'
    }
    Write-Verbose "Creating UDP Packet"
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 6)
    $packet += $mymac * 16
    Write-Verbose ([bitconverter]::tostring($packet))
    [void] $UDPclient.Send($packet, $packet.Length)
    Write-Host  "   - Wake-On-Lan Packet of length $($packet.Length) sent to $mymac"
}

Open in new window

0
 
LVL 70

Author Comment

by:Chris Dent
ID: 22822249

I thought it might be more complicated than that and didn't want to impose :)

That simplifies most things actually. I hadn't figured out why my attempt to cast with [Byte]$Object wasn't working, that extra [] to declare it as an array makes so much difference :) Less code is good.

Passes the time, and gives me silly things to blog about, PowerShell is such a lot of fun :)

Chris
0
 
LVL 70

Author Closing Comment

by:Chris Dent
ID: 31510658
Thank you :)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now