Jump to content

Recommended Posts

Posted

I have a powershell script that currently imports a csv, and runs a command to create a student against each line of it. It is as follows.

 

import-csv $Student |`

ForEach-Object {"for each student, create a user account"}

 

What I'd like to do though, is only run the creation of an account if the student doesn't exist. I'm unsure though how to go about adding in that conditional logic. If you cannot tell, I'm new to the world of piping and the like.

 

I even used to have it be Where{} instead of ForEach-Object and that worked fine too (just no conditional logic).

 

Sorry to be a dunce, but reading through an actual powershell book for answers isn't helping me "get it".

Posted

Since you haven't provided much detail, I'm going to assume you're talking AD. You would be best to feed your Imported CSV to a Where-Object first. Then you can query it against a boolean return, such as Get-QadUser. The below assumes your CSV username column is "username".

Import-Csv $Student | ? { ! (Get-QadUser $_.Username) } | % { "Create the user }

 

The pipeline flow goes something like this:

 

Import a list of new students as an object: Import-Csv $student

Where the user does not exist: ? {!(Get-QADUser $_.username)}

? = Where-Object

! = returns null

Get-QADUser = You need to install the Quest AD cmdlets to use this, but Get-ADUser will also work if you have RSAT installed

$_.Username = username field from the rows provided

 

Let me know if this makes sense...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...