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.
Get-Help about_Splattingfor the recommended way to do what you did with those nasty, icky, yucky backticks. [grin]