1

I have some code in PowerShell (below) to call the System.Guid constructor with a byte array (byte[]) as the only parameter.

The C# equivalent of this code is:

byte[] binaryData = userObj["ADGuid"].Value;
Guid adid = new System.Guid(binaryData);

This is my PowerShell code. It interprets the items array as individual parameters. How do I need to adjust this code?

[byte[]]$binaryData = $uo["ADGuid"].Value                 
$adid = new-object System.Guid -ArgumentList $binaryData

Here is a screenshot of the error message:

PowerShell Error Message

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73

1 Answers1

2

PowerShell is treating the array of bytes as a list of 16 individual parameters and trying to find a constructor for System.Guid that accepts that many, which it can't do because there isn't one.

If you want to pass a single parameter which just happens to be an array of 16 bytes you need to wrap it in a "sacrificial array" so PowerShell only sees one parameter for the constructor...

$adid = new-object System.Guid -ArgumentList @(, $binaryData)

In this case, the single parameter is an array of bytes, which it can find a constructor overload for (i.e. public Guid (byte[] b);).

mclayton
  • 8,025
  • 2
  • 21
  • 26
  • I've never heard of the `sacrificial array` -- i like it... its a bit clunky but it gets the job done – Glenn Ferrie Aug 05 '22 at 01:52
  • It’s not an official term - it’s one I heard in passing a while ago and it kind of stuck in my head :-). If you want a better description you might want to look at one of the answers in the duplicate questions - this one is pretty thorough… https://stackoverflow.com/a/49307779/3156906 – mclayton Aug 05 '22 at 04:42