Solved

Read True or False from CSV to execute powershell commands

Posted on 2010-09-09
6
561 Views
Last Modified: 2013-11-25
Guys,

After a bit of help here, I have a CSV that gets generated for new users added to the environment each week. In this CSV are some options that can either be true or false, for example; OCS.

Can someone give me an example of how I could execute an enable OCS user command for those users in the CSV that have True selected and ignore those who have False? There are some other aspects I need this code for as well, but OCS is the primary.

The code I have for provisioning so far is (much of the credit for this should go to Sedgwick):

Import-CSV c:\temp\users.csv | ForEach-Object {
      $counter=1
      $UPN="@rcc.local"
      $samaccount = "UP"+$_.Name.Substring(0,1)+$_.Initial.Substring(0,1)+$_.Surname.Substring(0,1)+$counter
      $name=$_.Name+" "+$_.Initial+" "+$_.Surname
      $filter = "(samaccountname=$samaccount)(name=$name)"
      $result = get-QADUser -SearchRoot 'test.local/Companies' -LdapFilter $filter
      while($result -ne $null){
            $counter=$counter+1
            $samaccount = "UP"+$_.Name.Substring(0,1)+$_.Initial.Substring(0,1)+$_.Surname.Substring(0,1)+$counter
            $name=$_.Name+" "+$_.Initial+" "+$_.Surname
            $filter = "(samaccountname=$samaccount)(name=$name)"
            $result = get-QADUser -SearchRoot 'test.local/Companies' -LdapFilter $filter
      }
      New-QADUser -Name $name -DisplayName $name - -UserPrincipalName $($samaccount + $UPN) -Department $_.Department -Title $_.Title -PhoneNumber $_.Phone -SamAccountName $samaccount -Initials $_.Initial -FirstName $_.Name -LastName $_.Surname -City $_.City -Company $_.Company -StreetAddress $_.StreetAddress -StateorProvince $_.State -PostalCode $_.PostCode -ObjectAttributes @{countryCode="36";c="AU";co="Australia"} -ParentContainer "OU=Companies,DC=test,DC=local"
      Add-QADGroupMember 'test\SEC_Test_GG' $samaccount
}

Cheers,

Nick
0
Comment
Question by:SCC-EE
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 33635455
You should insert this into your code:

Import-CSV c:\temp\users.csv | Where-Object {$_.ocs} | ForEach-Object {
...
This will filter out the users, that have True as OCS.
0
 

Author Comment

by:SCC-EE
ID: 33635532
Soostibi,

Thanks for the quick response. However I still want to create ALL users in AD, some just won't have OCS account. By filtering your way, I wouldn't be creating the users who don't have OCS.
0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33635642
Then you can insert in before the last } the following structure:
if($_.ocs){
  your OCS commands.
}
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:SCC-EE
ID: 33636095
Soostibi,

I have this for testing purposes:

if($_.ocs='TRUE'){
  Add-QADGroupMember 'test\SEC_Test_GG' $samaccount
}

I have values "TRUE" and "FALSE" in the CSV against each record, however this snipet dosn't seem to pick up the value and just populates the group with everyone in the CSV instead of just those with "TRUE" against them. You'll have to excuse my ignorance, I'm very much a powershell newbie.
0
 

Author Comment

by:SCC-EE
ID: 33636505
Worked it out myself, should have been -eq instead of =

if($_.ocs -eq "TRUE"){
  Add-QADGroupMember 'test\SEC_Test_GG' $samaccount
}

Thanks for all your help Soostibi.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33638480
actually, here it should be this, sorry:

if([bool] $_.ocs){
 ....
}
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
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…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

943 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now