Forum Discussion
Sushantjha
Sep 28, 2020Copper Contributor
bulk update extension attribute in AD
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 entr...
farismalaeb
Sep 29, 2020Steel Contributor
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
Sushantjha
Sep 29, 2020Copper Contributor
Still last iteration is saved to $Output and exported, I am not getting all the results.
- farismalaebSep 30, 2020Steel Contributor
Did it work ?
- farismalaebSep 29, 2020Steel Contributor
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