PowerShell - Pass Multiple Elements To Array Arguments In Function

Bobby Llih
Bobby Llih used Ask the Experts™
on
I need help passing elements to array arguments in function.

I have current script that will, accept hard-coded elements. This will change the grid color to black in Windows 7 task Manager.
$Bytes = [System.IO.File]::ReadAllBytes("C:\Windows\System32\taskmgr.exe")

$Offsets = @(0x0000B387, 0x0000FB84)
[Byte[]] $Hex_Color_Bytes = 0x00, 0x00, 0x00

For ($i = 0; $i -lt $Offsets.Length; $i++)
{
  For ($j = 0; $j -lt $Hex_Color_Bytes.Length; $j++)
  {
    $Bytes[$Offsets[$i]++] = $Hex_Color_Bytes[$j]
  }
}

[System.IO.File]::WriteAllBytes("C:\Windows\System32\taskmgr.exe", $Bytes)

Open in new window


This script does nothing? It will print the arguments, though.
$Bytes = [System.IO.File]::ReadAllBytes("C:\Windows\System32\taskmgr.exe")

Function Hex_Change_Colors([Array]$Offsets, [Byte[]]$Hex_Color_Bytes)
{
  For ($i = 0; $i -lt $Offsets.Length; $i++)
  {
    For ($j = 0; $j -lt $Hex_Color_Bytes.Length; $j++)
    {
      $Bytes[$Offsets[$i]++] = $Hex_Color_Bytes[$j]
    }
  }
  # Write-Host $Offsets, $Hex_Color_Bytes
}

Hex_Change_Colors 0x0000B387, 0x0000FB84, 0x00, 0xFF, 0x00
[System.IO.File]::WriteAllBytes("C:\Windows\System32\taskmgr.exe", $Bytes)

Open in new window


Can someone please help?

Many thanks in advance. (Apologies for confusion, new to PowerShell)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Consultant
Commented:
Change your function call to this:

Hex_Change_Colors @( 0x0000B387, 0x0000FB84 ) @( 0x00, 0xFF, 0x00 )

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
You do not need the @(), the commas alone are creating an array. The main point is to not use a comma between parameters - those are always separated by whitespace.
You can also use parameter names, which makes it more obvious:
Hex_Change_Colors -Offsets 0x0000B387, 0x0000FB84 -Hex_Co!or_Bytes 0x00, 0xFF, 0x00

Open in new window

Author

Commented:
Many Thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial