+ Post New Thread
Results 1 to 4 of 4
Windows Server 2008 R2 Thread, Powershell - ForEach with conditional logic? in Technical; I have a powershell script that currently imports a csv, and runs a command to create a student against each ...
  1. #1
    goaliepride's Avatar
    Join Date
    Feb 2008
    Posts
    47
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Powershell - ForEach with conditional logic?

    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".

  2. #2
    chrisbrown's Avatar
    Join Date
    Aug 2010
    Location
    Melbourne, Australia
    Posts
    103
    Thank Post
    2
    Thanked 16 Times in 14 Posts
    Rep Power
    17
    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".
    Code:
    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...

  3. #3
    goaliepride's Avatar
    Join Date
    Feb 2008
    Posts
    47
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    That helped me immensely, thanks Chris! ? { ! (Get-QadUser $_.Username) } was the key I was missing

  4. #4
    chrisbrown's Avatar
    Join Date
    Aug 2010
    Location
    Melbourne, Australia
    Posts
    103
    Thank Post
    2
    Thanked 16 Times in 14 Posts
    Rep Power
    17
    Quote Originally Posted by goaliepride View Post
    That helped me immensely, thanks Chris! ? { ! (Get-QadUser $_.Username) } was the key I was missing
    No worries, let me know if there's anything else.



SHARE:
+ Post New Thread

Similar Threads

  1. [Joke] Programmers logic
    By MK-2 in forum Jokes/Interweb Things
    Replies: 2
    Last Post: 22nd February 2011, 03:58 PM
  2. [MS Office - 2003] Conditional Formatting
    By stevenwba in forum Office Software
    Replies: 3
    Last Post: 17th December 2009, 03:28 PM
  3. [Website] Tricky 3D Logic Puzzle
    By Iain in forum Jokes/Interweb Things
    Replies: 7
    Last Post: 8th September 2009, 11:17 PM
  4. Lunchtime Logic Puzzle
    By _Bat_ in forum General Chat
    Replies: 21
    Last Post: 4th June 2009, 04:53 PM
  5. Help with conditional formatting
    By randle in forum Windows
    Replies: 3
    Last Post: 25th October 2007, 02:20 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •