Calling line in CSV to pipe into powershell argument

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Calling line in CSV to pipe into powershell argument

Post 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. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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,,,,
  11. 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,,,,
  12. 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,,,,
  13. 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. $OU = Import-CSV "C:\Temp\book1.csv"
  3. $OU = Where-Object { $_.RecipientOU }
  4. $fltr = '(|(objectCategory=person)' +
  5.     '(&(objectCategory=group)' +
  6.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  7. '(msExchResourceMetaData=ResourceType:Room))'
  8. Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
  9. Set-ADObject -add @{
  10. msExchExtensionCustomAttribute1 = 'TEST453'
  11. }

Thank you for your help
Last edited by Carpe_diem on Wed Aug 01, 2018 10:11 pm, edited 3 times in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post 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.
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Re: Calling line in CSV to pipe into powershell argument

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra »

Also note that the following line will always be null.
$OU = Where-Object { $_.RecipientOU }
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem »

I updated the post, Thank you
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra »

You still have that line that creates a null for $OU which is exactly what the error message is telling you.
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem »

Ok Thank you for that
  1. $OU = import-csv "c:\temp\book1.csv" | select RecipientOU
  2.     $fltr = '(|(objectCategory=person)' +
  3.     '(&(objectCategory=group)' +
  4.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  5.     '(msExchResourceMetaData=ResourceType:Room))'
  6.     Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
  7.     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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling line in CSV to pipe into powershell argument

Post 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.
User avatar
Carpe_diem
Posts: 22
Last visit: Thu Feb 10, 2022 1:50 pm

Re: Calling line in CSV to pipe into powershell argument

Post 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. 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
  3. 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. $ou.RecipientOU
  3. $DISTRICTCODE.ExchangeDistrictCode
  4.     $fltr = '(|(objectClass=contact)' +
  5.     '(&(objectCategory=group)' +
  6.     '(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
  7.     '(msExchResourceMetaData=ResourceType:Room))'
  8.    
  9.     Get-ADObject -LDAPFilter $fltr -SearchBase $ou -SearchScope Subtree |
  10.     Set-ADObject -add @{
  11.         msExchExtensionCustomAttribute1 = $_.'ExchangeDistrictCode'
  12.     }
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
Last edited by Carpe_diem on Sat Aug 04, 2018 10:09 am, edited 5 times in total.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked