Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Active Directory Powershell

Posted on 2014-09-29
7
169 Views
Last Modified: 2014-10-03
I have a pshell script that creates users based on the <NewQAD-User> method my problem is I have a csv file with a column name of "Logon Usrename" that has all the SAMAccounts and I would like the script to see if the `New Logon Names` exist in AD and then display if they are or not...
I have done some research and have managed to find something similar on the`Net' , however I need a little help as I dont know how to reference my "Logon Username" column in this script.
Please if you can assist me in this I would greatly appreciate it allot

<# Imran Pathan > Find if the user exist in AD #> 
    
$UserList=IMPORT-CSV C:\FINAL_SCRIPTS\findUsers.csv 
$DC = 'PDC002.mydomain.local' 
Connect-QADService -service $DC 
Function FindDomainUser 
{ 
    Param( 
        [string] $_SamAccountName 
        ) 
    #-------------------- Find user --------------------------# 
    if(Get-QADUser -SamAccountName $_SamAccountName) 
     { 
         Write-Host "User Exist - $_SamAccountName" 
     } 
     else 
     { 
         Write-Host "User Don't Exist - $_SamAccountName"  
     } 
} 
#------------------ Find user End --------------------------# 
FOREACH ($Person in $UserList) { 
    FindDomainUser $Person.SamAccountName 
}

Open in new window

0
Comment
Question by:Jason Laskey
  • 5
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 40349431
For properties which contain a space just encapsulate them in brackets.
FOREACH ($Person in $UserList) { 
    FindDomainUser $Person."Logon Username" 
}

Open in new window

0
 

Author Comment

by:Jason Laskey
ID: 40349447
Will try this during today that <learnctx> one more thing I would be greatly happy with is perhaps have a script like this that checks to see if the userlogon name exist and then if it does make it <userlogon name> +<1> and then once all the new users have been created display it either on the screen or <pipe> it to a txt file?
0
 

Author Comment

by:Jason Laskey
ID: 40349677
@ Learnctx My buddy it works I will award you the points but before I close do you perhaps have a solution for my above statement...ie.e< I would be greatly happy with is perhaps have a script like this that checks to see if the userlogon name exist and then if it does make it <userlogon name> +<1> and then once all the new users have been created display it either on the screen or <pipe> it to a txt file?> but anyway you are a legend as this was what I am looking for
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Expert Comment

by:Learnctx
ID: 40351463
I don't have the quest cmdlets, but using the AD cmdlets you could just implement a simple counter.

$Counter=$null
$ContinueScript=$true
$user="username"
While ($ContinueScript)
{
	$newuser="$($user)$($counter)"
	try {$x=Get-ADUser -Identity $newuser -ErrorAction Stop; "$newuser exists already";$counter++}catch{$ContinueScript=$false}
}
$newuser
## go on to create the user with new-qaduser or whatever the command is.

Open in new window


Something like the above. It will check for example jsmith. It will find jsmith and then the counter will increment by 1. It will keep searching for a new unique user name by incrementing the counter by 1 until say jsmith5 doesn't exist. It will then set $ContinueScript=$false and break out of the while loop.
0
 

Author Comment

by:Jason Laskey
ID: 40351666
Hi Buddy and thank you again for you assistance as much appreciated..just so that we are on the same page here though...<does this script run and then if it find that the username is already in use will it prompt you to type in a new username then and then create the `new username' after you type it in? Also I have custom attributes like tel number, address etc...will this still be amended to the new user name?
0
 

Author Comment

by:Jason Laskey
ID: 40351669
This is my script I am currently using
Set-ExecutionPolicy Unrestricted
Add-PSSnapin quest.activeroles.admanagement
Import-Csv "c:\bin\NewUsers.csv" | ForEach-Object {
 $userPrinc = $_."Logon Username" + "@contoso.com"
 New-QADUser -Name $_.Name `
 -ParentContainer $_."Container" `
 -SamAccountName $_."Logon Username" `
 -UserPassword "Password01" `
 -FirstName $_."First Name" `
 -LastName $_."Last Name" `
 -physicalDeliveryOfficeName $_."Office" `
 -telephoneNumber $_."Telephone Number" `
 -streetAddress $_."Street Address" `
 -l $_."City" `
 -st $_."State" `
 -postalCode $_."Postal Code" `
 -facsimileTelephoneNumber $_."Fax Number" `
 -title $_."Job Title" `
 -department $_."Department" `
 -company $_."Company" `
 -manager $_."Manager" `
 -Description $_."Job Title" `
 -UserPrincipalName $userPrinc `
 -DisplayName $_."Name" ;`
 Add-QADGroupMember -identity $_."Email Group 1" -Member $_."Logon Username" ;`
 Add-QADGroupMember -identity $_."Email Group 2" -Member $_."Logon Username" ;`
 Add-QADGroupMember -identity $_."Email Group 3" -Member $_."Logon Username" ;`
 Add-QADGroupMember -identity $_."Email Group 4" -Member $_."Logon Username" ;`
 Add-QADGroupMember -identity $_."Email Group 5" -Member $_."Logon Username" ;`
 Set-QADUser -identity $_."Logon Username" `
 -UserMustChangePassword $true `
}

Open in new window

0
 

Author Closing Comment

by:Jason Laskey
ID: 40358897
This worked! Thats allot for your assistance in this task!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question