gpt4 book ai didi

powershell - 用于创建新用户的学生脚本,错误:System.String,类型为“System.Management.Automation.SwitchParameter”

转载 作者:行者123 更新时间:2023-12-03 01:26:52 26 4
gpt4 key购买 nike

我正在从.csv中提取一些用户信息以创建新用户,
在这里有人建议下,我将“新用户参数”设置为“小”

但我收到此错误

New-ADUser : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 
'Confirm'.
At C:\Users\Administrator\Documents\GitHub\cyclone-internal-user-sync-1\Bamboo Attributes form a csv.ps1:68 char:28
+ New-ADUser @NewUserParms
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.NewADUser

我不知道这是什么哈哈,我曾尝试向新广告用户添加一个erroraction stop,但这没有任何效果

我添加了修饰和一个部分以从用户名中删除空格。处理诸如Van der等多部分名称
#Bamboo Attributes from a .csv

#Enter a path to your import CSV file
$ADUsers = Import-csv 'path'


#Bamboo Attributes from a .csv

#Enter a path to your import CSV file
$ADUsers = Import-csv 'C:\Users\Administrator\Documents\GitHub\cyclone-internal-user-sync-1\documentation\SampleUserAttributes.csv'
#$apiRequest = Get-Content -Raw -Path C:\Users\alexh\Documents\GitHub\cyclone-internal-user-sync-1\cyclone-internal-user-sync-1\fake-api-query.json | ConvertFrom-Json

foreach ($User in $ADUsers) {


$firstName = $user.FirstName.Trim()
$surname = $user.Surname.Trim()

$vaildUsernameFormat = "[^a-zA-Z_.]" # identifies anything that's _not_ a-z or underscore or .
$username = "($firstName'.'$surname)" -replace $vaildUsernameFormat, '' #removes anything that isn't a-z

$DefaultPassword = 'Pa$$w0rd'

$NewUserParms = @{
'samAccountName' = $username;
'Name' = "$firstname $surname";
'DisplayName' = "$firstname $surname";
'UserPrincipalName' = "$username@domain.com";
'GivenName' = $firstname;
'Surname' = $surname;
'EmailAddress' = $User.Email;
'AccountPassword' = (ConvertTo-SecureString $DefaultPassword -AsPlainText -Force);
'Enabled' = $true;
'Path' = "OU=Users,DC=domain,DC=com";
'co' = $User.Country;
'company' = $User.CompanyName;
'countryCode' = $user.countryCode;
'department' = $user.OrgDepartmentName;
'Employeeid' = $user.EmployeeId;
'exstentionAttribute1' = $user.ExstentionNumber;
'ipPhone' = $user.ExstentionNumber;
'L' = $user.location;
'mail' = $user.Email;
'mobile' = $user.Mobile;
'Manager' = $user.Manager;
'physicalDeliveryOffice' = $user.Branch;
'postalCode' = $user.PostalCode;
'postOfficeBox' = $user.PostOfficeBox;
'proxyAddresses' = $user.ProxyEmail;
'scriptPath' = $user.scriptPath;
'st' = $user.StreetName;
'Title' = $user.Title

}




write-host "$username this is username value"



#Check if the user account already exists in AD
if (Get-ADUser -F {
sAMAccountName -eq $username
}) {
#If user does exist, output a warning message
Write-Warning "A user account $username has already exist in Active Directory."

}
else {
#If a user does not exist then create a new user account

New-ADUser @NewUserParms


}
}




我删除了一些用户属性,只是为了使其更小一些。
这也是the.csv,以防万一我搞砸了
link to .csv file on git

最佳答案

关于PowerShell的一个鲜为人知的事实是,您不需要使用整个参数名称。您可以使用部分名称,只要它仅与一个参数名称匹配,这就是PowerShell假定的意思。

令人窒息的是:

            'co'                     = $User.Country;

如果您查看 the documentation for New-ADUser ,它没有名为 co的参数。因此,PowerShell假定它是与已知参数的部分匹配,而最接近的匹配是 -Confirm。而且 $User.Country中的值对于 -Confirm参数没有任何意义,因此会引发错误。

您将必须使用 -OtherAttributes参数来设置 New-ADUser没有专用于以下参数的所有其他属性:
$NewUserParms = @{
...

'OtherAttributes = @ {
'co' = $User.Country;
'exstentionAttribute1' = $user.ExstentionNumber;
...
}

...
}

关于powershell - 用于创建新用户的学生脚本,错误:System.String,类型为“System.Management.Automation.SwitchParameter”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com