Solved

Read True or False from CSV to execute powershell commands

Posted on 2010-09-09
6
560 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now