Calling line in CSV to pipe into powershell argument

Ask your Windows 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.
User avatar
Carpe_diem
Posts: 22
Joined: Thu Jan 10, 2013 8:35 am

Calling line in CSV to pipe into powershell argument

Post by Carpe_diem » Wed Aug 01, 2018 4:18 pm

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

Code: Select all

[[Import-CSV C:\Temp\book1.csv | ForEach-Object { New-MailContact -Name $_.DisplayName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit $_.RecipientOU]]

the csv file looks like this

Code: Select all

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
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
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
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
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
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
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
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
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
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,,,,
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,,,,
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,,,,
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.

Code: Select all


$OU = Import-CSV "C:\Temp\book1.csv"
$OU = Where-Object { $_.RecipientOU }
$fltr = '(|(objectCategory=person)' +
'(&(objectCategory=group)' +
'(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
'(msExchResourceMetaData=ResourceType:Room))'
Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
Set-ADObject -add @{
msExchExtensionCustomAttribute1 = 'TEST453'
}

Thank you for your help
Last edited by Carpe_diem on Wed Aug 01, 2018 10:11 pm, edited 3 times in total.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra » Wed Aug 01, 2018 4:32 pm

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
Joined: Thu Jan 10, 2013 8:35 am

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem » Wed Aug 01, 2018 9:32 pm

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

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra » Wed Aug 01, 2018 9:38 pm

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.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra » Wed Aug 01, 2018 9:48 pm

Also note that the following line will always be null.
$OU = Where-Object { $_.RecipientOU }

User avatar
Carpe_diem
Posts: 22
Joined: Thu Jan 10, 2013 8:35 am

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem » Wed Aug 01, 2018 10:06 pm

I updated the post, Thank you

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra » Wed Aug 01, 2018 10:10 pm

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
Joined: Thu Jan 10, 2013 8:35 am

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem » Thu Aug 02, 2018 7:58 am

Ok Thank you for that

Code: Select all

$OU = import-csv "c:\temp\book1.csv" | select RecipientOU
$fltr = '(|(objectCategory=person)' +
'(&(objectCategory=group)' +
'(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
'(msExchResourceMetaData=ResourceType:Room))'
Get-ADObject -LDAPFilter $fltr -SearchBase $OU -SearchScope Subtree
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

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling line in CSV to pipe into powershell argument

Post by jvierra » Thu Aug 02, 2018 8:04 am

$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
Joined: Thu Jan 10, 2013 8:35 am

Re: Calling line in CSV to pipe into powershell argument

Post by Carpe_diem » Fri Aug 03, 2018 3:59 pm

got it to work Thank you


pulling from

Code: Select all

DisplayName,Alias,RecipientType,ExchangeDistrictCode,RecipientOU,ExternalEmailAddress,Distribution Group,Distribution Group Primary SMTP address,Attribute,Distribution Group Managers,Distribution Group Type
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
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

Code: Select all

$OUlist = Import-CSV "C:\Temp\book1.csv"
$ou.RecipientOU
$DISTRICTCODE.ExchangeDistrictCode
$fltr = '(|(objectClass=contact)' +
'(&(objectCategory=group)' +
'(!(groupType:1.2.840.113556.1.4.803:=2147483648)))' +
'(msExchResourceMetaData=ResourceType:Room))'

Get-ADObject -LDAPFilter $fltr -SearchBase $ou -SearchScope Subtree |
Set-ADObject -add @{
msExchExtensionCustomAttribute1 = $_.'ExchangeDistrictCode'
}
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.

Locked