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.
ScriptBlockbased filters for AD Cmdlets even tho it might work is not really supported. You should use query string as recommended by Microsoft. As an example of what can happen when usingScriptBlock: stackoverflow.com/questions/70126195/…