Need help with Byte arrays

Need some help with Byte arrays and setting logon hours. I have these arrays:

[byte[]]$logonHoursPST = @(0,0,0,0,192,255,7,192,255,7,192,255,7,192,255,7,192,255,7,0,0)
[byte[]]$logonHoursMST = @(0,0,0,0,192,255,3,192,255,3,192,255,3,192,255,3,192,255,3,0,0)
[byte[]]$logonHoursCST = @(0,0,0,0,224,255,1,224,255,1,224,255,1,224,255,1,224,255,1,0,0)
[byte[]]$logonHoursEST = @(0,0,0,0,240,255,0,240,255,0,240,255,0,240,255,0,240,255,0,0,0)

I then have another array that I can easily enumerate, and hopefully grab my byte array depending on the TZ entered by the user

$aryTimeZones = @(
      ('PST', $logonHoursPST),
      ('MST', $logonHoursMST),
      ('CST', $logonHoursCST),
      ('EST', $logonHoursEST)

$TZ = Read-Host -Prompt 'Input the users Time Zone, PST, MST, CST, EST'
for($i=0; $i -lt $aryTimeZones.Count; $i++) {
     if($aryTimeZones[$i][0] -eq $TZ) {
          $TZ = $aryTimeZones[$i][1]
          $validTZ = $True

However, when I actually try to set the logon hours, it fails (sorry, don’t recall the exact error message just now):

Set-ADUser -Identity $userName -Replace @{logonhours = $TZ}

If I use the initial array declaration, it works:

Set-ADUser -Identity $userName -Replace @{logonhours = $logonHoursPST}

Please point me to the error of my ways. Thanks in advance.

I would use switch statement to simplify the logic as in:

$TZ = Read-Host -Prompt 'Input the user''s Time Zone, PST, MST, CST, EST'
switch ($TZ.ToUpper()) {
    'PST' { $logonHours = @(0,0,0,0,192,255,7,192,255,7,192,255,7,192,255,7,192,255,7,0,0) }
    'MST' { $logonHours = @(0,0,0,0,192,255,3,192,255,3,192,255,3,192,255,3,192,255,3,0,0) }
    'CST' { $logonHours = @(0,0,0,0,192,255,7,192,255,7,192,255,7,192,255,7,192,255,7,0,0) }
    'EST' { $logonHours = @(0,0,0,0,224,255,1,224,255,1,224,255,1,224,255,1,224,255,1,0,0) }
    default { 'Input the user''s Time Zone, PST, MST, CST, EST' }

if ($logonHours) { Set-ADUser -Identity $userName -Replace @{logonhours = $logonHours} }

Your code might be failing because you’re re-using $TZ as the variable receiving the user input and the one getting the Byte array assignment, especially if/when the input is not the right ‘case’ as pST or pst instead of PST.

Thanks Sam, I will give that a wack and see how it goes. I also think you meant this, am I correct, or don’t I need to define the variable as a BYTE array?

‘PST’ {[BYTE[]]$logonHours = @(0,0,0,192,255,7,192,255,7,192,255,7,192,255,7,192,255,7,0,0)}