Question : PowerShell: Combining arrays


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
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
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 Select All

Answer : PowerShell: Combining arrays

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
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
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 Select All
Random Solutions  
 
programming4us programming4us