Script to find and replace using a csv

TAslan 20 Reputation points
2023-05-24T20:10:27.1666667+00:00

Hi, I'm trying to create a script that when given a variable, it will search through a CSV and use the values that match the given variable's line. For example, $variable = US:

csv file contains a list of all countries and their corresponding countrycodes and co attribute for LDAP

co c countryCode
United States US 840
Uruguay UY 858

Then I want to set those variables as attributes for

set-aduser -identity me -replace @{c=US ;co=United States ;countryCode=840}

I have this but it gives the error 'The argument is null or an element of the argument collection contains a null value.'


$CN = "US"
$list = import-csv 'C:\temp\ISO-Country Codes List.csv'
foreach ($list in $lists)  {  
$lookup = $lists.c
if ($lookup -eq $CN){

$params= @{  
                        c = $list.c  
                        Co = $list.co
                        countrycode = $list.countrycode


                        
                        }
				}
		}

set-aduser -identity me -replace @{co=$params.co;countrycode=$params.countrycode}


Hope this makes sense and thanks in advance for your help!

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2023-05-25T03:16:17.0366667+00:00

    I think this is what you're asking for:

    $Script:CN = "US"
    $Script:co = ""
    $Script:countrycode = ""
    $Script:CFound = $false
    
        # c = 2-letter ISO countrycode (e.g., AU). This is the "Country" property in a Get-ADUser
        # co = Country name (e.g., Australia)
        # countrycode = ISO 3166 country code (e.g., 36)
    Import-Csv 'C:\temp\ISO-Country Codes List.csv' |
        ForEach-Object{
            if ($_.c -eq $Script:CN){
                $Script:co = $_.co
                $Script:countrycode = $_.countrycode
                $Script:CFound = $true
            }
        }
    if ($CFound){
        Get-ADUser -Filter "c -eq '$Script:CN'" -Properties * |      # return only users with the country value found in $CN
            ForEach-Object{
                $_ | Set-ADUser  -replace @{co=$Script:co; countrycode=$Script:countrycode}
            }
    }
    else{
        Write-Error "CSV file does not contain the 2-letter country code '$CN' found in the `$CN parameter"
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 44,746 Reputation points
    2023-05-25T12:29:19.09+00:00
    Hello there,
    
    Here's one way - get the column names from the input table, iterate each row in the table, and output new custom objects with needed changes.
    
    $table = Import-Csv "Test.csv"
    
    # Get column names
    $columnNames = ($table | Select-Object -First 1).PSObject.Properties |
      Select-Object -ExpandProperty Name
    
    # Iterate each row in the table
    foreach ( $row in $table ) {
      $outputObject = New-Object PSObject
      foreach ( $columnName in $columnNames ) {
        if ( $columnName -eq "Username" ) {
          $outputObject | Add-Member NoteProperty "Username" ($row.Username.Split('\')[1])
        }
        else {
          $outputObject | Add-Member NoteProperty $columnName $row.$columnName
        }
      }
      $outputObject
    }
    
    To create a new CSV file as output, put the above code in a script and pipe to Export-Csv.
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer--
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.