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"}
}
asked Aug 2, 2017 at 16:12
\$\endgroup\$

1 Answer 1

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
}
answered Aug 3, 2017 at 5:20
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.