Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 尝试使用format运算符创建变量,但变量的数据发生更改_Arrays_Variables_Active Directory_Powershell 5.0 - Fatal编程技术网

Arrays 尝试使用format运算符创建变量,但变量的数据发生更改

Arrays 尝试使用format运算符创建变量,但变量的数据发生更改,arrays,variables,active-directory,powershell-5.0,Arrays,Variables,Active Directory,Powershell 5.0,所以,我又回到了以前玩过的游戏中。它自诞生以来发生了很大的变化。但是,它基本上仍然从CSV中提取数据,并根据需要在AD中创建帐户。它运行得很好,但当我们发现有人没有中间名时,我遇到了麻烦。中间的首字母是密码格式的一部分。所以,我尝试了一些方法,包括回收我的一些产品,但我似乎无法用正确的密码创建广告帐户。该帐户已创建,但我无法使用该帐户应接受的凭据进行身份验证 我已经添加了我觉得相关的代码。这并不是整个脚本,因为它有200多行代码,而且似乎运行良好,但是,如果您想全部查看,请告诉我,我将编辑以下代

所以,我又回到了以前玩过的游戏中。它自诞生以来发生了很大的变化。但是,它基本上仍然从CSV中提取数据,并根据需要在AD中创建帐户。它运行得很好,但当我们发现有人没有中间名时,我遇到了麻烦。中间的首字母是密码格式的一部分。所以,我尝试了一些方法,包括回收我的一些产品,但我似乎无法用正确的密码创建广告帐户。该帐户已创建,但我无法使用该帐户应接受的凭据进行身份验证

我已经添加了我觉得相关的代码。这并不是整个脚本,因为它有200多行代码,而且似乎运行良好,但是,如果您想全部查看,请告诉我,我将编辑以下代码段

只要孩子有一个中间的首字母,代码段就可以工作:

第一次尝试绕过缺少的中间首字母:

第二次尝试绕过缺少的中间首字母:


如果我运行ADUserParams,我可以看到AccountPassword参数是System.Security.SecureString,所以我认为这是一件好事。那么,我做错了什么?我想这两种方法中的任何一种都能奏效——只要我把一切都搞定。但是,正如我所说,除非我恢复到旧代码,否则我无法进行身份验证。旧代码不能处理没有中间首字母的帐户。

我认为您的顺序有点错误。在定义变量$FirstName和$LastName之前,您正在使用变量$FirstName和$LastName创建$AccountPass

这应该行得通

# CSV file being imported.
$CsvFile = "$env:USERPROFILE\Downloads\SampleData.csv"

# Import the contents of the CSV file.
$Users = Import-Csv -Path "$CsvFile"

# Loop through each line of the CSV, creating the user if the account doesn't already exist in AD.
ForEach ($User in $Users) {
    [String]$LoginName = $User.'Stu Access Login'
    If (-Not (Get-ADUser -Server $ADServer -Filter {SamAccountName -eq $LoginName})) {
        $FirstName = $User.'Student First Name'
        $LastName = $User.'Student Last Name'

        # generate passwords
        If (!([String]::IsNullOrEmpty($User.I))) {
            # this person has an initial to use in the password
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}{2}#{3}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $User.I[0].ToString().ToLower(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force
        } 
        Else {
            # this person does not have an initial to work with
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}#{2}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force
        }

        $ADUserParams = @{
            Name = "$FirstName $LastName"
            SamAccountName = $LoginName
            GivenName = $FirstName
            Initials = $User.'I'
            Surname = $LastName
            DisplayName = "$FirstName $($User.'I') $LastName"
            Description= $SchoolCodes[$User.School].Name
            ScriptPath= "student.bat"
            UserPrincipalName = "$LoginName@academic.mydomain.k12.pa.us"
            EmailAddress = "$LoginName@mydomain.org"
            Company = "$LoginName@mydomain.org"
            EmployeeID = $User.'Other ID'
            HomeDirectory = "$FileServer\$LoginName"
            HomeDrive = "Z:"
            AccountPassword = $AccountPass
            Enabled = $True
            PasswordNeverExpires = $True
            CannotChangePassword = $True
            Path = 'OU={0},OU=Students,OU={1},OU=accounts,DC=academic,DC=mydomain,DC=k12,DC=pa,DC=us' -f @(
                $User.'Grad Year',
                $SchoolCodes[$User.School].Name)
            Server = $ADServer
            WhatIf = $False
        } # End ADUserParams

        Try {
            # Create new user.
            New-ADUser @ADUserParams -Verbose -ErrorAction Stop
        }

        Catch {
            # If there's an error, write the error to the event log.
            Write-EventLog -LogName $EventLogName -Source $EventLogSources[0][1] -EntryType Warning -EventId $EventLogSources[0][0] -Message "Something went wrong with the creation of a new user, [$LoginName] : $_"
        }}}

p、 对于组合Displayname,我认为这样做可能更好:$FirstName$$User.I'$LastName.Trim-replace'\s+',''。它避免了在没有$$用户时使用双空格!这正是为什么一双新眼睛派上用场的原因。一天中的大部分时间我都在为这个争论。真不敢相信我没看到。。。谢谢你的帮助。是的,我想找出一个干净的方法,当用户没有中间的首字母时,清理显示名称,所以我也会尝试一下你的想法。再次感谢!
# CSV file being imported.
$CsvFile = "$env:USERPROFILE\Downloads\SampleData.csv"

# Import the contents of the CSV file.
$Users = Import-Csv -Path "$CsvFile"

# Loop through each line of the CSV, creating the user if the account doesn't already exist in AD.
ForEach ($User in $Users) {
    [String]$LoginName = $User.'Stu Access Login'
    If (-Not (Get-ADUser -Server $ADServer -Filter {SamAccountName -eq $LoginName})) {
        # Attempt #1 for Dealing with passwords for people without a middle initial.
        IF([String]::IsNullOrEmpty($User.I)) {
            $AccountPass = '{0}{1}#{2}' -f @(
                $FirstName[0].ToString().ToUpper(),
                $LastName[0].ToString().ToLower(),
                $User.'Other ID')
            } Else {
            $AccountPass = '{0}{1}{2}#{3}' -f @(
                $FirstName[0].ToString().ToUpper(),
                $User.I[0].ToString().ToLower(),
                $LastName[0].ToString().ToLower(),
                $User.'Other ID')
            }
        $FirstName = $User.'Student First Name'
        $LastName = $User.'Student Last Name'
        $ADUserParams = @{
            Name = "$FirstName $LastName"
            SamAccountName = $LoginName
            GivenName = $FirstName
            Initials = $User.'I'
            Surname = $LastName
            DisplayName = "$FirstName $($User.'I') $LastName"
            Description= $SchoolCodes[$User.School].Name
            ScriptPath= "student.bat"
            UserPrincipalName = "$LoginName@academic.mydomain.k12.pa.us"
            EmailAddress = "$LoginName@mydomain.org"
            Company = "$LoginName@mydomain.org"
            EmployeeID = $User.'Other ID'
            HomeDirectory = "$FileServer\$LoginName"
            HomeDrive = "Z:"
            AccountPassword = (ConvertTo-SecureString -String $AccountPass -AsPlainText -Force)
            Enabled = $True
            PasswordNeverExpires = $True
            CannotChangePassword = $True
            Path = 'OU={0},OU=Students,OU={1},OU=accounts,DC=academic,DC=mydomain,DC=k12,DC=pa,DC=us' -f @(
                $User.'Grad Year',
                $SchoolCodes[$User.School].Name)
            Server = $ADServer
            WhatIf = $False
        } # End ADUserParams

        Try {
            # Create new user.
            New-ADUser @ADUserParams -Verbose -ErrorAction Stop
        }

        Catch {
            # If there's an error, write the error to the event log.
            Write-EventLog -LogName $EventLogName -Source $EventLogSources[0][1] -EntryType Warning -EventId $EventLogSources[0][0] -Message "Something went wrong with the creation of a new user, [$LoginName] : $_"
        }}}
# CSV file being imported.
$CsvFile = "$env:USERPROFILE\Downloads\SampleData.csv"

# Import the contents of the CSV file.
$Users = Import-Csv -Path "$CsvFile"

# Loop through each line of the CSV, creating the user if the account doesn't already exist in AD.
ForEach ($User in $Users) {
    [String]$LoginName = $User.'Stu Access Login'
    If (-Not (Get-ADUser -Server $ADServer -Filter {SamAccountName -eq $LoginName})) {
        # Attempt #2 for Dealing with passwords for people without a middle initial.
        If ($User.I -ne "") {
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}{2}#{3}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $User.I[0].ToString().ToLower(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force

        } Else {
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}#{2}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force
        } # End If
        $FirstName = $User.'Student First Name'
        $LastName = $User.'Student Last Name'
        $ADUserParams = @{
            Name = "$FirstName $LastName"
            SamAccountName = $LoginName
            GivenName = $FirstName
            Initials = $User.'I'
            Surname = $LastName
            DisplayName = "$FirstName $($User.'I') $LastName"
            Description= $SchoolCodes[$User.School].Name
            ScriptPath= "student.bat"
            UserPrincipalName = "$LoginName@academic.mydomain.k12.pa.us"
            EmailAddress = "$LoginName@mydomain.org"
            Company = "$LoginName@mydomain.org"
            EmployeeID = $User.'Other ID'
            HomeDirectory = "$FileServer\$LoginName"
            HomeDrive = "Z:"
            AccountPassword = $AccountPass
            Enabled = $True
            PasswordNeverExpires = $True
            CannotChangePassword = $True
            Path = 'OU={0},OU=Students,OU={1},OU=accounts,DC=academic,DC=mydomain,DC=k12,DC=pa,DC=us' -f @(
                $User.'Grad Year',
                $SchoolCodes[$User.School].Name)
            Server = $ADServer
            WhatIf = $False
        } # End ADUserParams

        Try {
            # Create new user.
            New-ADUser @ADUserParams -Verbose -ErrorAction Stop
        }

        Catch {
            # If there's an error, write the error to the event log.
            Write-EventLog -LogName $EventLogName -Source $EventLogSources[0][1] -EntryType Warning -EventId $EventLogSources[0][0] -Message "Something went wrong with the creation of a new user, [$LoginName] : $_"
        }}}
# CSV file being imported.
$CsvFile = "$env:USERPROFILE\Downloads\SampleData.csv"

# Import the contents of the CSV file.
$Users = Import-Csv -Path "$CsvFile"

# Loop through each line of the CSV, creating the user if the account doesn't already exist in AD.
ForEach ($User in $Users) {
    [String]$LoginName = $User.'Stu Access Login'
    If (-Not (Get-ADUser -Server $ADServer -Filter {SamAccountName -eq $LoginName})) {
        $FirstName = $User.'Student First Name'
        $LastName = $User.'Student Last Name'

        # generate passwords
        If (!([String]::IsNullOrEmpty($User.I))) {
            # this person has an initial to use in the password
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}{2}#{3}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $User.I[0].ToString().ToLower(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force
        } 
        Else {
            # this person does not have an initial to work with
            $AccountPass = ConvertTo-SecureString -String (
                '{0}{1}#{2}' -f @(
                    $FirstName[0].ToString().ToUpper(),
                    $LastName[0].ToString().ToLower(),
                    $User.'Other ID')) -AsPlainText -Force
        }

        $ADUserParams = @{
            Name = "$FirstName $LastName"
            SamAccountName = $LoginName
            GivenName = $FirstName
            Initials = $User.'I'
            Surname = $LastName
            DisplayName = "$FirstName $($User.'I') $LastName"
            Description= $SchoolCodes[$User.School].Name
            ScriptPath= "student.bat"
            UserPrincipalName = "$LoginName@academic.mydomain.k12.pa.us"
            EmailAddress = "$LoginName@mydomain.org"
            Company = "$LoginName@mydomain.org"
            EmployeeID = $User.'Other ID'
            HomeDirectory = "$FileServer\$LoginName"
            HomeDrive = "Z:"
            AccountPassword = $AccountPass
            Enabled = $True
            PasswordNeverExpires = $True
            CannotChangePassword = $True
            Path = 'OU={0},OU=Students,OU={1},OU=accounts,DC=academic,DC=mydomain,DC=k12,DC=pa,DC=us' -f @(
                $User.'Grad Year',
                $SchoolCodes[$User.School].Name)
            Server = $ADServer
            WhatIf = $False
        } # End ADUserParams

        Try {
            # Create new user.
            New-ADUser @ADUserParams -Verbose -ErrorAction Stop
        }

        Catch {
            # If there's an error, write the error to the event log.
            Write-EventLog -LogName $EventLogName -Source $EventLogSources[0][1] -EntryType Warning -EventId $EventLogSources[0][0] -Message "Something went wrong with the creation of a new user, [$LoginName] : $_"
        }}}