locked
How to get my script to append the values in the CSV file for a value that is not found? RRS feed

  • Question

  • I have been trying to get a simple script together to import details of various teams from a master file and then user their samaccountname to query AD for further details. However, some of the samaccountnames have changed, and these are just being ignored. I need the script to output an entry to the CSV file it exports to in order that we know that this specific AD user account was not found. I know this is very basic, but after Googling other people's scripts, what I found was not really suitable for cannibalizing code. If someone can direct me on how I can do this, that would be really helpful.

    Script as below:

    $team='wintel'
    $users = import-csv "C:\temp\$team.csv"
    $users | ForEach-Object {Get-AdUser -filter "samaccountname -eq '$($_.StandardAccount)'" -properties memberof} `
    | Select-Object Name, SamAccountName,UserPrincipalName,@{n='memberOf';e={$_.memberOf -join '; '}} | Export-Csv "C:\temp\Results\Standard\$team.csv"

    Thursday, October 4, 2018 1:39 PM

Answers

  • $team='wintel'
    import-csv "C:\temp\$team.csv" | 
        ForEach-Object {
            if($user = Get-AdUser -filter "samaccountname -eq '$($_.StandardAccount)'" -properties memberof){
                $user
            }else{
                Write-Host "NOT FOUND: $($_.StandardAccount)" -fore red
            }
        } |
        Select-Object Name, SamAccountName,UserPrincipalName,@{n='memberOf';e={$_.memberOf -join '; '}} | 
        Export-Csv "C:\temp\Results\Standard\$team.csv"


    \_(ツ)_/

    Thursday, October 4, 2018 2:06 PM

All replies

  • Please post code correctly using the code posting tool provided.  Also please format code correctly so that it is readable.


    \_(ツ)_/

    Thursday, October 4, 2018 2:02 PM
  • $team='wintel'
    import-csv "C:\temp\$team.csv" | 
        ForEach-Object {
            if($user = Get-AdUser -filter "samaccountname -eq '$($_.StandardAccount)'" -properties memberof){
                $user
            }else{
                Write-Host "NOT FOUND: $($_.StandardAccount)" -fore red
            }
        } |
        Select-Object Name, SamAccountName,UserPrincipalName,@{n='memberOf';e={$_.memberOf -join '; '}} | 
        Export-Csv "C:\temp\Results\Standard\$team.csv"


    \_(ツ)_/

    Thursday, October 4, 2018 2:06 PM