Error: Missing statement block after if ( condition )

Welcome Forums General PowerShell Q&A Error: Missing statement block after if ( condition )

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 week, 1 day ago.

  • Author
    Posts
  • #160638

    Participant
    Topics: 1
    Replies: 3
    Points: -20
    Rank: Member
    #Enter a path to your import CSV file
    $ADUsers = Import-csv C:\scripts\newusers.csv
    
    foreach ($User in $ADUsers)
    {
    
           $Username    = $User.username
           $Password    = $User.password
           $Firstname   = $User.firstname
           $Lastname    = $User.lastname
        $Department = $User.department
           $OU           = $User.ou
    
           #Check if the user account already exists in AD
           if (Get-ADUser -F {SamAccountName -eq $Username}) -SearchBase "DC=XXX,DC=NET"
           {
                   #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
              
            #Account will be created in the OU listed in the $OU variable in the CSV file; don't forget to change the domain name in the"-UserPrincipalName" variable
                  New-ADUser `
                -SamAccountName $Username `
                -UserPrincipalName "$Username@yourdomain.com" `
                -Name "$Firstname $Lastname" `
                -GivenName $Firstname `
                -Surname $Lastname `
                -Enabled $True `
                -ChangePasswordAtLogon $True `
                -DisplayName "$Lastname, $Firstname" `
                -Department $Department `
                -Path $OU `
                -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
    
           }
    }
  • #160646

    Moderator
    Topics: 8
    Replies: 884
    Points: 2,716
    Helping Hand
    Rank: Community Hero

    -SearchBase "DC=XXX,DC=NET" should be inside the ().

    if(Get-ADUser -F {SamAccountName -eq $Username} -SearchBase "DC=XXX,DC=NET"){
    
    }
    
  • #160656

    Participant
    Topics: 1
    Replies: 3
    Points: -20
    Rank: Member

    Thanks so much for your quick reply.  Unfortunately, I'm now getting the following error:

    Get-ADUser : Error parsing query: 'SamAccountName -eq $Username'

    Error Message: 'Operator Not supported: ' at position: '16

     

    Thanks!

  • #160659

    Participant
    Topics: 2
    Replies: 442
    Points: 920
    Helping Hand
    Rank: Major Contributor

    Check your query string and ensure that -eq is written with a hyphen and not some other dash character that just looks really similar. There are a few of those.

     

  • #160668

    Participant
    Topics: 1
    Replies: 3
    Points: -20
    Rank: Member

    Sir, thanks for much for your help, that seemed to resolve some other issues I was having.  I am now getting the following error:

    New-ADUser: A paramater cannot be found that matches paramenter name 'Gecos'

    -Gecos <<<< $gecos `   I believe gecos is a valid parameter for New-ADUser so I don't understand what problem is with this line...    

  • #160673

    Moderator
    Topics: 8
    Replies: 884
    Points: 2,716
    Helping Hand
    Rank: Community Hero

    gecos...what's that !.

    Please read the documentation for New-AdUser to see the parameters and examples.

    Get-Help New-AdUser -Online
  • #160691

    Participant
    Topics: 1
    Replies: 3
    Points: -20
    Rank: Member

    Sorry, I was looking at a different script using OtherAtrributes...thanks!  How do I give you both credit?

You must be logged in to reply to this topic.