bulk update extension attribute in AD

%3CLINGO-SUB%20id%3D%22lingo-sub-1721289%22%20slang%3D%22en-US%22%3Ebulk%20update%20extension%20attribute%20in%20AD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1721289%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22kvgmc6g5%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3E%3CDIV%3EI%20have%20written%20below%20script%20to%20update%20the%20extension%20attribute%20and%20after%20updating%20I%20want%20the%20report%20in%20CSV.%20If%20I%20run%20till%20update%20it%20is%20working%20fine%20but%20when%20I%20am%20adding%20select-object%20to%20take%20the%20entries%20in%20CSV%20it%20is%20throwing%20an%20error.%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22o9v6fnle%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%20ii04i59q%22%3E%3CDIV%3EImport-Csv%20-path%20c%3A%5Ctemp%5CSetExtAtt1.csv%20%7CForEach-Object%20%7B%3C%2FDIV%3E%3CDIV%3ESet-ADComputer%20%24_.samAccountName%20-replace%20%40%7B%3C%2FDIV%3E%3CDIV%3EExtensionAttribute4%20%3D%20%22%24(%24_.ExtensionAttribute4)%22%3C%2FDIV%3E%3CDIV%3EExtensionAttribute6%20%3D%20%22%24(%24_.ExtensionAttribute6)%22%7CSelect-Object%20%40%7BLabel%20%3D%20%22Name%22%3BExpression%20%3D%20%7B%24_.samAccountName%7D%7D%2C%3C%2FDIV%3E%3CDIV%3E%40%7BLabel%20%3D%20%22Attribute4%22%3BExpression%20%3D%20%7B%24_.extensionAttribute4%7D%7D%2C%3C%2FDIV%3E%3CDIV%3E%40%7BLabel%20%3D%20%22Attribute6%22%3BExpression%20%3D%20%7B%24_.extensionAttribute6%7D%7D%7CExport-Csv%20C%3A%5CTemp%5COwnerTest2.csv%20-Append%20-NoTypeInformation%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1721289%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EActive%20Directory%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1722148%22%20slang%3D%22en-US%22%3ERe%3A%20bulk%20update%20extension%20attribute%20in%20AD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1722148%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F812162%22%20target%3D%22_blank%22%3E%40Sushantjha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20Export%20the%20Hash%20table%20you%20will%20need%20to%20use%20the%20GetEnumerator()%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftommymaynard.com%2Fhash-table-to-csv-2018%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftommymaynard.com%2Fhash-table-to-csv-2018%2F%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20recommend%20that%20you%20replace%20the%20hash%20table%20with%20PSObject%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1723062%22%20slang%3D%22en-US%22%3ERe%3A%20bulk%20update%20extension%20attribute%20in%20AD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1723062%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F790105%22%20target%3D%22_blank%22%3E%40farismalaeb%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20made%20the%20below%20changes%20but%20e%3CSPAN%3Each%20iteration%20of%20foreach%20loop%20saves%20to%20%24Output.%20It%20overwriting%20what%20was%20there%20previously%2C%20i.e.%2C%20the%20previous%20iteration.%20Which%20means%20that%20only%20the%20very%20last%20iteration%20is%20saved%20to%20%24Output%20and%20exported.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22kvgmc6g5%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%22%3E%3CDIV%3EImport-Csv%20-path%20c%3A%5Ctemp%5CSetExtAtt1.csv%20%7CForEach-Object%20%7B%3C%2FDIV%3E%3CDIV%3E%24Output%20%3D%20%40()%3C%2FDIV%3E%3CDIV%3ESet-ADComputer%20%24_.samAccountName%20-replace%20%40%7B%3C%2FDIV%3E%3CDIV%3EExtensionAttribute4%20%3D%20%22%24(%24_.ExtensionAttribute4)%22%3C%2FDIV%3E%3CDIV%3EExtensionAttribute6%20%3D%20%22%24(%24_.ExtensionAttribute6)%22%7D%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22o9v6fnle%20cxmmr5t8%20oygrvhab%20hcukyx3x%20c1et5uql%22%3E%3CDIV%3E%24Output%20%3DNew-Object%20-TypeName%20PSObject%20-Property%20%40%7B%3C%2FDIV%3E%3CDIV%3EsamAccountName%20%3D%20%22%24(%24_.samAccountName)%22%3C%2FDIV%3E%3CDIV%3EExtensionAttribute4%20%3D%20%22%24(%24_.ExtensionAttribute4)%22%3C%2FDIV%3E%3CDIV%3EExtensionAttribute6%20%3D%20%22%24(%24_.ExtensionAttribute6)%22%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3CDIV%3E%24Output%20%7C%20Export-Csv%20C%3A%5CTemp%5COwnerTest2.csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Append%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1723999%22%20slang%3D%22en-US%22%3ERe%3A%20bulk%20update%20extension%20attribute%20in%20AD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1723999%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F812162%22%20target%3D%22_blank%22%3E%40Sushantjha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20sure%20as%20the%20%24Output%3D%40()%20is%20inside%20the%20loop%20and%20is%20being%20reset%20everytime%2C%20a%20very%20quick%20fix%20will%20be%20to%20add%20a%20new%20variable%20at%20the%20top%3C%2FP%3E%3CP%3Eand%20once%20the%20%24output%20have%20the%20result%2C%20add%20it%20to%20the%20variable%3C%2FP%3E%3CP%3Etry%20the%20code%20below%20and%20get%20the%20result%20from%20%24allresult%20variable.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24allresult%3D%40()%0AImport-Csv%20-path%20C%3A%5C123%5CEXDB1.csv%20%7CForEach-Object%20%7B%0A%24Output%20%3D%20%40()%0ASet-ADComputer%20%24_.samAccountName%20-replace%20%40%7B%0AExtensionAttribute4%20%3D%20%22%24(%24_.ExtensionAttribute4)%22%0AExtensionAttribute6%20%3D%20%22%24(%24_.ExtensionAttribute6)%22%7D%0A%24Output%20%3DNew-Object%20-TypeName%20PSObject%20-Property%20%40%7B%0AsamAccountName%20%3D%20%22%24(%24_.samAccountName)%22%0AExtensionAttribute4%20%3D%20%22%24(%24_.ExtensionAttribute4)%22%0AExtensionAttribute6%20%3D%20%22%24(%24_.ExtensionAttribute6)%22%0A%24allresult%3D%24output%0A%7D%0A%7D%0A%24Output%20%7C%20Export-Csv%20C%3A%5CTemp%5COwnerTest2.csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Append%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor
I have written below script to update the extension attribute and after updating I want the report in CSV. If I run till update it is working fine but when I am adding select-object to take the entries in CSV it is throwing an error.
Import-Csv -path c:\temp\SetExtAtt1.csv |ForEach-Object {
Set-ADComputer $_.samAccountName -replace @{
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"|Select-Object @{Label = "Name";Expression = {$_.samAccountName}},
@{Label = "Attribute4";Expression = {$_.extensionAttribute4}},
@{Label = "Attribute6";Expression = {$_.extensionAttribute6}}|Export-Csv C:\Temp\OwnerTest2.csv -Append -NoTypeInformation
}
}
6 Replies
Highlighted

@Sushantjha 

To Export the Hash table you will need to use the GetEnumerator()

https://tommymaynard.com/hash-table-to-csv-2018/

I recommend that you replace the hash table with PSObject

 

Highlighted

@farismalaeb 

 

I have made the below changes but each iteration of foreach loop saves to $Output. It overwriting what was there previously, i.e., the previous iteration. Which means that only the very last iteration is saved to $Output and exported.

 

Import-Csv -path c:\temp\SetExtAtt1.csv |ForEach-Object {
$Output = @()
Set-ADComputer $_.samAccountName -replace @{
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"}
$Output =New-Object -TypeName PSObject -Property @{
samAccountName = "$($_.samAccountName)"
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"
}
}
$Output | Export-Csv C:\Temp\OwnerTest2.csv -NoTypeInformation -Encoding UTF8 -Append
Highlighted

@Sushantjha 

Yes, sure as the $Output=@() is inside the loop and is being reset everytime, a very quick fix will be to add a new variable at the top

and once the $output have the result, add it to the variable

try the code below and get the result from $allresult variable.

$allresult=@()
Import-Csv -path C:\123\EXDB1.csv |ForEach-Object {
$Output = @()
Set-ADComputer $_.samAccountName -replace @{
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"}
$Output =New-Object -TypeName PSObject -Property @{
samAccountName = "$($_.samAccountName)"
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"
$allresult=$output
}
}
$Output | Export-Csv C:\Temp\OwnerTest2.csv -NoTypeInformation -Encoding UTF8 -Append

 

Highlighted

@farismalaeb 

 

Still last iteration is saved to $Output and exported, I am not getting all the results.

Highlighted

@Sushantjha 

Yes, becuase I forget to update the last line, as the CSV exporting the output not the $allreasult.

Check the updated

 

 

$allresult=@()
Import-Csv -path C:\123\EXDB1.csv |ForEach-Object {
$Output = @()
Set-ADComputer $_.samAccountName -replace @{
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"}
$Output =New-Object -TypeName PSObject -Property @{
samAccountName = "$($_.samAccountName)"
ExtensionAttribute4 = "$($_.ExtensionAttribute4)"
ExtensionAttribute6 = "$($_.ExtensionAttribute6)"
}
$allresult+=$output
}
$allresult | Export-Csv C:\Temp\OwnerTest2.csv -NoTypeInformation -Encoding UTF8 -Append

 

 

Highlighted

@Sushantjha 

Did it work ?