• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

Read True or False from CSV to execute powershell commands

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
SCC-EE
Asked:
SCC-EE
  • 3
  • 3
1 Solution
 
soostibiCommented:
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
 
SCC-EEAuthor Commented:
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
 
soostibiCommented:
Then you can insert in before the last } the following structure:
if($_.ocs){
  your OCS commands.
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SCC-EEAuthor Commented:
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
 
SCC-EEAuthor Commented:
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
 
soostibiCommented:
actually, here it should be this, sorry:

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

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now