1

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.

1
  • 1
    Note that, using ScriptBlock based 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 using ScriptBlock: stackoverflow.com/questions/70126195/… Commented Dec 7, 2021 at 18:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.