Sep 28 2020 10:21 AM
Sep 28 2020 01:31 PM
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
Sep 29 2020 01:13 AM
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.
Sep 29 2020 04:34 AM
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
Sep 29 2020 05:06 AM
Still last iteration is saved to $Output and exported, I am not getting all the results.
Sep 29 2020 06:09 AM - edited Sep 29 2020 06:16 AM
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