Reputation: 21
I am complete newbie in the IT world but having the following question: I am exporting users from AD based on some criteria (code is messy but works). When I export the CSV file before the foreach section it all works well, but then I need to add 9 as a digit in front of all lines which start with 0 in my joined column for extensionattribute 1 and 2. I am trying to import the CSV file and then export it again edited.
The code:
$cost = @{Name="Cost";Expression={$_.extensionattribute1,$_.extensionattribute2 -join ''}}
$id = @{Name="SAP ID";Expression={$_.samaccountname}}
get-aduser -properties samaccountname,co,extensionattribute1,extensionattribute2 -filter {(co -eq "Dxxxx") -and (enabled -eq "true") -and (samaccountname -like "1*" -or samaccountname -like "2*" -or samaccountname -like "3*")} | select $id,$cost | export-csv C:\Users\xxxx\Favorites\CCDxxx.csv -notypeinformation -encoding UTF8
$csv = import-csv C:\Users\xxxx\Favorites\CCDxxx.csv
$var = foreach($line in $csv){
if($cost -like "0*"){
$newcost = "9"+$cost
write-host $sam ";" $newcost}
if($cost -notlike "0*"){
$newcost = $cost
write-host $sam ";" $cost}
}
$var | export-csv C:\Users\xxxx\Favorites\CCxxx1.csv -notypeinformation -encoding UTF8
The script will run but my lack of knowledge exports nothing. Any help will be highly appreciated.
P.S. Forgive me if I asked my question out of the high standards you have here, but I will learn. Thank you, Regards.
Upvotes: 1
Views: 273
Reputation: 437208
You need to refer to each object being enumerated in your foreach
loop in order to inspect and update its .cost
property:
if($line.cost -like "0*"){
# ...
$line.cost = ...
}
Your loop needn't produce output (no need for $var = ...
), as you can simply use $csv
as the input to the 2nd Export-Csv
call, given that $csv
after the loop contains the modified objects.[1]
However, you can streamline your code by using only a single pipeline in which you transform the Get-ADUser
output objects as desired with the help of a ForEach-Object
call, in whose script block ({ ... }
) you can use the automatic $_
variable to refer to the input object at hand. In lieu of Select-Object
with calculated properties, you can construct the desired output objects with a [pscustomobject]
literal.
get-aduser -properties samaccountname,co,extensionattribute1,extensionattribute2 -filter {(co -eq "Dxxxx") -and (enabled -eq "true") -and (samaccountname -like "1*" -or samaccountname -like "2*" -or samaccountname -like "3*")} |
ForEach-Object {
$cost = $_.extensionattribute1, $_.extensionattribute2 -join ''
if ($cost -notlike '0*') { $cost = '9' + $cost }
# Create and output a custom object with the desired properties.
[pscustomobject] @{
Cost = $cost
'SAP ID' = $_.samaccountname
}
} |
export-csv C:\Users\xxxx\Favorites\CCDxxx.csv -notypeinformation -encoding UTF8
[1] [pscustomobject]
is a .NET reference type, which means that the $csv
array contains references to [pscustomobject]
instances, so that later modifications of the properties of these instances are also reflected in the array.
Upvotes: 3