[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Read True or False from CSV to execute powershell commands

Posted on 2010-09-09
6
Medium Priority
?
568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 33635642
Then you can insert in before the last } the following structure:
if($_.ocs){
  your OCS commands.
}
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

650 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