2
\$\begingroup\$

This code below is for one of my scripts that I am trying to automate. I am dealing with the rows and all the data that is in those rows. What I am trying to do is make this look neater and maybe adding some different variables to make it look decent.

Foreach ($row in $csv) {
If ($row.Type0 -eq 'Domain') { 
  $row."Unique Account Name" = "$($row.Domain0) - $($row.Account0)" 
If ($row."Unique Account Name" -in @('ACCOUNTS - DODSCAN'.'ACCOUNTS - Domain Admins','ACCOUNTS - LADM_WS_Admins','ACCOUNTS - Tech Enterprise'))
        {$row."Excluded" = "True"}
    Else {$row."Excluded" = "False"}        
 }
Else {
    $row."Unique Account Name" = "$($row.Netbios_name0) - $($row.Account0)"
    If ($row."Account0" -in @('esrxadm1n_esi','#Update','medco_tech','medco_admin'))
         {$row."Excluded" = "True"}
  Else {$row."Excluded" = "False"}
}
\$\endgroup\$
2
\$\begingroup\$
  • Indent the blocks properly.
  • Define the immutable exclusion lists before the loop.
  • To enhance readability, maintainability, and to avoid missing typos such as the dot after DODSCAN, which should be a comma, write just one array element per line without commas.

$excludedAccounts = @{
    unique = @(
        'ACCOUNTS - DODSCAN'
        'ACCOUNTS - Domain Admins'
        'ACCOUNTS - LADM_WS_Admins'
        'ACCOUNTS - Tech Enterprise'
    )
    account0 = @(
        'esrxadm1n_esi'
        '#Update'
        'medco_tech'
        'medco_admin'
    )
}

foreach ($row in $csv) {
    if ($row.Type0 -eq 'Domain') { 
        $newName = "$($row.Domain0) - $($row.Account0)" 
        $isRowExcluded = $newName -in $excludedAccounts.unique
    } else {
        $newName = "$($row.Netbios_name0) - $($row.Account0)"
        $isRowExcluded = $row.'Account0' -in $excludedAccounts.account0
    }
    $row.'Unique Account Name' = $newName
    $row.'Excluded' = [string]$isRowExcluded
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.