0

I am attempting to create a powershell script to maintain users. Current environment. Office365 with ADFS, AD and on premise exchange server 2010. All integrated together. For the current problem, I am trying to import a list of users from a csv, and apply them to a specific distribution group, in either exchange or Active Directory. Both sync and update each other in my environment. I would like to do this based on the employee # of the employee, which is stored as extensionattribute5 in Active Directory. I would also like to create a success/fail log. Following is my script. Following that is the error message. The test file contains two columns, Display Name, and EmpNo. What am I doing wrong here/missing. Can't seem to get this to work.

    import-module activedirectory

$Users=Import-csv c:\test.csv 
$failedUsers = @()
$usersAlreadyExist =@()
$successUsers = @()
$VerbosePreference = "Continue"
$LogFolder = "C:\temp"

ForEach($User in $Users)
{


try {

    if (!(get-aduser -Filter "extensionattribute5 -like '$($_.EmpNo)'")){ 
        Add-ADGroupMember -Identity 'IMS S3' -Member SAMAccountName
        Write-Verbose "[PASS] Created $EmpNo"
        $successUsers += $EmpNo
    }
    else {
        Write-Warning "[WARNING] [$($EmpNo)] already exists in Distribution Group"
        $usersAlreadyExist += $EmpNo
    }
}
catch {
    Write-Warning "[ERROR]Can't create add [$($EmpNo)] : $_"
    $failedUsers += $EmpNo
}
}
if ( !(test-path $LogFolder)) {
    Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
    new-item $LogFolder -Force 
}

Write-verbose "Writing logs"
$failedUsers | out-file -FilePath  $LogFolder\FailedUsers.log -Force -Verbose
$usersAlreadyExist | out-file -FilePath  $LogFolder\usersAlreadyExist.log -Force -Verbose
$successUsers | out-file -FilePath  $LogFolder\successUsers.log -Force -Verbose

Error message follows: At C:\users\new3.ps1:16 char:72 + ... if (!(get-aduser -Filter "extensionattribute5 -like '$($_.EmpNo)'")) + ~ Missing statement block after if ( condition ). + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : MissingStatementBlock

1 Answers1

0

You have a single quote that is quoting the "-like" comparison operator with the extensionattribute in the Get-Aduser Filter. You need a double quote before the -like, and remove the double quote after the single quote in the comparison:

import-module activedirectory

$Users=Import-csv c:\test.csv 
$failedUsers = @()
$usersAlreadyExist =@()
$successUsers = @()
$VerbosePreference = "Continue"
$LogFolder = "C:\temp"

ForEach($User in $Users)
{


try {

    if (!(get-aduser -Filter "extensionattribute5" -like '$($_.EmpNo)')){ 
        Add-ADGroupMember -Identity 'IMS S3' -Member SAMAccountName
        Write-Verbose "[PASS] Created $EmpNo"
        $successUsers += $EmpNo
    }
    else {
        Write-Warning "[WARNING] [$($EmpNo)] already exists in Distribution Group"
        $usersAlreadyExist += $EmpNo
    }
}
catch {
    Write-Warning "[ERROR]Can't create add [$($EmpNo)] : $_"
    $failedUsers += $EmpNo
}
}
if ( !(test-path $LogFolder)) {
    Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
    new-item $LogFolder -Force 
}

Write-verbose "Writing logs"
$failedUsers | out-file -FilePath  $LogFolder\FailedUsers.log -Force -Verbose
$usersAlreadyExist | out-file -FilePath  $LogFolder\usersAlreadyExist.log -Force -Verbose
$successUsers | out-file -FilePath  $LogFolder\successUsers.log -Force -Verbose
Davidw
  • 1,222
  • 3
  • 14
  • 25