设置登录时间失败

时间:2016-01-05 01:07:55

标签: powershell active-directory

我想设置很多用户'通过PowerShell登录小时(v2我相信)。需要应用的用户和登录时间列表是从制表符分隔的文本文件中提取的。这是一个示例(Logonhours已被截断,但不在实际文件中:

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"

这是我目前的代码:

$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}
}

Get-ADUser正在运行,每行中的Logonhours似乎已正确转换为数组。但是当我运行整个脚本时,我收到以下错误:

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

上周我能够设置所有用户&#39;使用此代码登录小时:

$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}
}

我不确定本周代码的问题所在。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

请尝试以下调整:

$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}
}

拆分以逗号分隔的字符串会创建一个字符串数组,而logonhours属性则需要一个字节数组。