Solved

Read True or False from CSV to execute powershell commands

Posted on 2010-09-09
6
563 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
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

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A simple overview of the possibilities of using technology for project management.
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 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