-1

so I'm trying to process improve, and automate new hires......However my foreachloop is not working and for some reason the variables are giving the follow errors

Set-ADUser : Identity info provided in the extended attribute: 'Manager' could not be resolved. Reason: 'Cannot find an object with identity: 'Manager Name' under: 'DC=DC,DC=DC'.'.
At C:\Users\ajimmy\Documents\Powershell\Visual Studio\NewHireSpreadsheet.ps1:69 char:5
+     Set-ADUser -Identity $UN  -Title $Ti -Department $DT -Company $CY ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (userid:ADUser) [Set-ADUser], ADIdentityResolutionException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityResolutionException,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Set-ADUser : add
At C:\Users\ajimmy\Documents\Powershell\Visual Studio\NewHireSpreadsheet.ps1:71 char:5
+     Set-ADUser $UN -Add @{"EmployeeID"="$EID"}
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (userid:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Exception calling "GetSteppablePipeline" with "1" argument(s): "Cannot bind argument to parameter 'TokenExpiryTime' because it is null."
At C:\Users\ajimmy\AppData\Local\Temp\tmp_n13adieb.0bj\tmp_n13adieb.0bj.psm1:7395 char:13
+             $steppablePipeline = $scriptCmd.GetSteppablePipeline($myI ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CmdletInvocationException

Here is my code:

$Users = import-csv -path "\\Server\Folder\Folder\Folder\NewHire.csv";
    
Import-Module ActiveDirectory -Force;
Import-Module RemoteDesktop -Force;
Import-Module ExchangeOnlineManagement -Force;
Import-Module AzureAD -Force;
Import-Module Sqlserver -Force;
    
$OnPrem = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "serveraddress"  -Authentication Kerberos
Import-PSSession $OnPrem | Out-Null
    
Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.SelectVoice('Microsoft Zira Desktop')
$speak.Rate = 1.5
    
Connect-ExchangeOnline;
Connect-MsolService;
    
foreach ($User in $Users) {
    $department = $User.Department;
    
    switch -Regex -Exact ($department)
    {
        CareCenter {
    
            $FN = $User.FirstName
            $LN = $User.LastName
            $UN = $User.Username
            $PW = $User.Password
            $MN = $User.Manager
            $DT = $User.Department
            $CY = $User.Company
            $EID = $User.EmployeeID
       
            $Email = $User.Username + "@email.com"
            $Ti = $User.Title
     
            New-ADUser `
                -Name "$FN $LN" `
                -GivenName "$FN" `
                -Surname "$LN" `
                -SamAccountName "$UN" `
                -UserPrincipalName "$Email" `
                -AccountPassword (ConvertTo-SecureString "$PW" -AsPlainText -Force) `
                -Path "path" `
                -ChangePasswordAtLogon 1 `
                -Enabled 1 
    
            Set-ADUser -Identity $UN  -Title $Ti -Department $DT -Company $CY -Manager { Name -is $MN }
            Set-ADUser $UN -EmailAddress "[email protected]" 
            Set-ADUser $UN -Add @{"EmployeeID" = "$EID" }
    
            Get-ADUser -Identity "ADTmp-CCRep" -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $UN
    
            Enable-RemoteMailbox -identity "$FN $LN" -RemoteRoutingAddress "[email protected]"
    
            Set-MsolUser -UserPrincipalName $email -UsageLocation US 
            Set-MsolUserLicense -UserPrincipalName $email -AddLicenses "server:SPE_E3"
    
            break;
    
        }

        Default { write-host ("Department Title Not Found") }
    
    }

The Idea is process the spreadsheet and then have it circle each item to find what department its assigned to the create ad accounts, grant adgroups based on a template account and then create onprem mailbox and exchangeonline items. soon i will be adding sql items but i want to get this working first.

2
  • For the manager, if it using a SamAccountName attribute, you need to populate it using the distinguished name. See this answer Commented Apr 22, 2022 at 16:45
  • 1
    [1] why on earth are you making those two-letter variables? [that is very very very bad practice, by the way.] you are just introducing a chance for an error. ///// [2] please strop using backticks for line wraps. take a look at Get-Help about_Splatting for the recommended way to do what you did with those nasty, icky, yucky backticks. [grin] Commented Apr 22, 2022 at 18:34

1 Answer 1

0

Powershell doesn't accept manager as plain text, try below

$MN = Get-aduser ($User.Manager)

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.