Page 1 of 2

Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 4:18 pm
by Carpe_diem
I am trying to extract one line from a CSV file and then use this line to change an attribute for all "Contacts" in this OU . I know there will be many duplicates in this line and the script might run 40 times on the same OU. and I am not sure how to fix that

my first script (which I already have) creates contacts from the same csv file, after completion I would like to set the attribute for all contacts in that OU the same
  1. [[Import-CSV C:\Temp\book1.csv | ForEach-Object { New-MailContact -Name $_.DisplayName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit $_.RecipientOU]]

the csv file looks like this
  1. DisplayName,Alias,RecipientType,RecipientOU,ExternalEmailAddress,Distribution Group,Distribution Group Primary SMTP address,Distribution Group Managers,Distribution Group OU,Distribution Group Type,Distribution Group Recipient Type
  2.  
  3. John_Doe_ABC.com,John_Doe_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,John_Doe@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  4.  
  5. SAME_Test_McTest1_ABC.com,Test_McTest1_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_McTest1@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  6.  
  7. Test_McTest1_ABC.com,Test_McTest1_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_McTest1@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  8.  
  9. Test_T_O'User1_ABC.com,Test_T_O'User1_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_T_O'User1@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  10.  
  11. Test_User1_ABC.com,Test_User1_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_User1@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  12.  
  13. Test_User2_ABC.com,Test_User2_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_User2@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  14.  
  15. Test_User4_ABC.com,Test_User4_ABC.com,MailContact,bc.com/FDO/National Projects/ABC/Contacts,Test_User4@ABC.com,bgroup_ABC_Test_Mail,bgroup_ABC_Test_Mail@bc.com,,bc.com/FDO/National Projects/ABC/Groups,Universal,MailUniversalDistributionGroup
  16.  
  17. Test_User5_ABC.com,Test_User5_ABC.com,MailContact,bc.com/FDO/National Projects/DEF/Contacts,Test_User5@DEF.com,bgroup_DEF_Test_Mail,bgroup_DEF_Test_Mail@bc.com,,bc.com/FDO/National Projects/DEF/Groups,Universal,MailUniversalDistributionGroup
  18.  
  19. Test_User7_DEF.com,Test_User7_DEF.com,MailContact,bc.com/FDO/National Projects/DEF/Contacts,Test_User7@DEF.com,bgroup_DEF_Test_Mail,bgroup_DEF_Test_Mail@bc.com,,,,
  20.  
  21. Test_User8_DEF.com,Test_User8_DEF.com,MailContact,bc.com/FDO/National Projects/DEF/Contacts,Test_User8@DEF.com,bgroup_DEF_Test_Mail,bgroup_DEF_Test_Mail@bc.com,,,,
  22.  
  23. Test_User9_DEF.com,Test_User9_DEF.com,MailContact,bc.com/FDO/National Projects/DEF/Contacts,Test_User9@DEF.com,bgroup_DEF_Test_Mail,bgroup_DEF_Test_Mail@bc.com,,,,
  24.  
  25. Test_User10_DEF.com,Test_User10_DEF.com,MailContact,bc.com/FDO/National Projects/DEF/Contacts,Test_User10@DEF.com,bgroup_DEF_Test_Mail,bgroup_DEF_Test_Mail@bc.com,,,,
below is the script I am trying but it produces the following error:

Get-ADObject : Cannot validate argument on parameter 'SearchBase'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:1 char:44
+ Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
+ ~~~
+ CategoryInfo : InvalidData: (:) [Get-ADObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADObject

[PS] C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Exchange Server 2016>Set-ADObject -add @{msExchExtensionCustomAttribute1 = 'TEST453'}

cmdlet Set-ADObject at command pipeline position 1
Supply values for the following parameters:
Identity:



Any idea what I am missing , I am trying to incorporate the filter to only change Contacts, Rooms etc in the OU not users if there would be any.

  1.  
  2.  
  3. $OU = Import-CSV "C:\Temp\book1.csv"
  4.  
  5. $OU = Where-Object { $_.RecipientOU }
  6.  
  7. $fltr = '(|(objectCategory=person)' +
  8.  
  9.     '(&(objectCategory=group)' +
  10.  
  11.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  12.  
  13. '(msExchResourceMetaData=ResourceType:Room))'
  14.  
  15. Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
  16.  
  17. Set-ADObject -add @{
  18.  
  19. msExchExtensionCustomAttribute1 = 'TEST453'
  20.  
  21. }

Thank you for your help

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 4:32 pm
by jvierra
You file is not a CSV file. Make it a CSV file and see what happens.

Please post your script correctly using the script posting tool supplied.

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 9:32 pm
by Carpe_diem
I am not sure what you mean , the file is a csv file
C:\Temp\book1.csv I was just in the example trying to show which line I am looking to grab,
Thank you for looking at it

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 9:38 pm
by jvierra
A CSV file has commas delimiting the fields. Your file will not load correctly because there are no field separators. First you need to fix the CSV file then post the code correctly so I can read it and copy it. As posted it is not usable code. You also need to post the complete error message and not just a piece of it.

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 9:48 pm
by jvierra
Also note that the following line will always be null.
$OU = Where-Object { $_.RecipientOU }

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 10:06 pm
by Carpe_diem
I updated the post, Thank you

Re: Calling line in CSV to pipe into powershell argument

Posted: Wed Aug 01, 2018 10:10 pm
by jvierra
You still have that line that creates a null for $OU which is exactly what the error message is telling you.

Re: Calling line in CSV to pipe into powershell argument

Posted: Thu Aug 02, 2018 7:58 am
by Carpe_diem
Ok Thank you for that
  1. $OU = import-csv "c:\temp\book1.csv" | select RecipientOU
  2.  
  3.     $fltr = '(|(objectCategory=person)' +
  4.  
  5.     '(&(objectCategory=group)' +
  6.  
  7.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  8.  
  9.     '(msExchResourceMetaData=ResourceType:Room))'
  10.  
  11.     Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
  12.  
  13.     Set-ADObject -add @{ msExchExtensionCustomAttribute1 = 'TEST777' }
that change moved it forward but the output of $OU and now it is stuck at the filter $fltr -SearchBase

Get-ADObject : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'SearchBase'. Specified method is not supported.
At line:1 char:44
+ Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADObject

Thank you for your hints

Re: Calling line in CSV to pipe into powershell argument

Posted: Thu Aug 02, 2018 8:04 am
by jvierra
$OU is now a collection of objects which cannot be used. Also your OUs in the file are canonical and SearchBase requires a Distinguished name for the OU.

Re: Calling line in CSV to pipe into powershell argument

Posted: Fri Aug 03, 2018 3:59 pm
by Carpe_diem
got it to work Thank you


pulling from
  1. DisplayName,Alias,RecipientType,ExchangeDistrictCode,RecipientOU,ExternalEmailAddress,Distribution Group,Distribution Group Primary SMTP address,Attribute,Distribution Group Managers,Distribution Group Type
  2.  
  3. John_Doe_ABC.com,John_Doe_ABC.com,MailContact,ABV,"""OU=Contacts,OU=ABC,OU=tdf,OU=ABC,DC=rd,DC=com""",John_Doe@ABC.com,ABCml_ABC_Test_Mail,ABCml_ABC_Test_Mail@rd.com,444,,Universal
  4.  
  5. SAME_Test_McTest1_ABC.com,Test_McTest1_ABC.com,MailContact,DSEF,"""OU=Contacts,OU=ABC,OU=thg,OU=ABC,DC=rd,DC=com""",Test_McTest1@ABC.com,ABCml_ABC_Test_Mail,ABCml_ABC_Test_Mail@rd.com,444,,Universal
  1. $OUlist = Import-CSV "C:\Temp\book1.csv"
  2.  
  3. $ou.RecipientOU
  4.  
  5. $DISTRICTCODE.ExchangeDistrictCode
  6.  
  7.     $fltr = '(|(objectClass=contact)' +
  8.  
  9.     '(&(objectCategory=group)' +
  10.  
  11.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  12.  
  13.     '(msExchResourceMetaData=ResourceType:Room))'
  14.  
  15.    
  16.  
  17.     Get-ADObject -LDAPFilter $fltr -SearchBase $ou -SearchScope Subtree |
  18.  
  19.     Set-ADObject -add @{
  20.  
  21.         msExchExtensionCustomAttribute1 = $_.'ExchangeDistrictCode'
  22.  
  23.     }
running it
makes the changes, so far only if I change msExchExtensionCustomAttribute1 to a value. It does not get the districtcode based on the csv value.

Thank you