razzle_dazzle_84
razzle_dazzle_84

Reputation: 55

Set Logon hours fails

I am trying to set many users' logon hours via PowerShell (v2 I believe). The list of users and logon hours that need to be applied are pulled from a tab-delimited text file. Here's a sample (the Logonhours have been truncated here but are not in the actual file:

Name   Logonhours
John Doe    "255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255..."
ishield "255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255..."
Jane Doe    "255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255..."
Tom Thumb "255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255..."
Tinker Bell "0,0,0,0,224,255,1,224,255,1,224,255,1,224,255,1,224,255,1,0,0"

Here's my current code:

$in = Import-CSV -Delimiter "`t" .\logonhours-initial_2.txt

foreach ($line in $in) {
    $Filter = "displayName -like ""*$($line.Name)*"""
    $user = Get-ADUser -Filter $Filter
    [array]$logonhours_new = ($line.Logonhours).split(",") | Where-Object {$_ -ne " "}
    Set-ADUser -Identity $user.SamAccountName -Replace @{logonhours = $logonhours_new}
}

The Get-ADUser is working and the Logonhours in each line appear to be properly converted into an array. But when I run the whole script, I get the following error:

Set-ADUser : Invalid type 'System.Management.Automation.PSObject'.
Parameter name: logonhours
At line:5 char:15
+     Set-ADUser <<<<  -Identity $user.SamAccountName -Replace @{logonhours = $logonhours_new}
    + CategoryInfo          : InvalidArgument: (jdoe:ADUser) [Set-ADUser], ArgumentException
    + FullyQualifiedErrorId : Invalid type 'System.Management.Automation.PSObject'.
    Parameter name: logonhours,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Last week I was able to set all users' logon hours by using this code:

$template = "jdoe"
$ou = "OU=someOU,DC=domain,DC=local"

# Get the logonhours from the template user
$template = Get-ADUser $template -Properties logonhours
[array]$logonHours = $template.logonhours

# Get all users
$users = Get-ADUser -SearchBase $ou -Filter {enabled -eq $true}

# Loop through all the users
foreach($user in $users){
    Set-ADUser -Identity $user.samaccountname -Replace @{logonhours = $logonHours}
}

I'm not sure where the problem lies with this week's code. Any help is appreciated.

Upvotes: 1

Views: 2337

Answers (1)

Sam
Sam

Reputation: 543

Please try the following adjustment:

$in = Import-CSV -Delimiter "`t" .\logonhours-initial_2.txt
foreach ($line in $in) {
    $Filter = "displayName -like ""*$($line.Name)*"""
    $user = Get-ADUser -Filter $Filter
    [byte[]]$logonhours_new = ($line.Logonhours).split(",") | Where-Object {$_ -ne " "}
    Set-ADUser -Identity $user.SamAccountName -Replace @{logonhours = $logonhours_new}
}

Splitting a comma-delimited string creates an array of strings, whereas the logonhours attribute expects a byte array.

Upvotes: 1

Related Questions