0
votes

I am very new to powershell and am running into a road block I can not figure out. I have a CSV with a list of 380 user accounts with one white space behind their samaccount name. This is on a Windows 2003 server that has PSv2 and Quest.Activeroles.admanagement This is what I have so far

`add-pssnapin quest.activeroles.admanagement

get-PSSnapin Quest.ActiveRoles.ADManagement

Import-Csv "C:\Documents and Settings\%user%\Desktop\withspaces2.csv" | ForEach{ Set-QADUser $_.sAMAccountName.Replace(' ',"").Replace("\t","") }`

It is removing the white space for the first user but doing nothing to the others here is what the Errors looks like.

Name                           Type            DN                                

---- ---- --
ABPR user CN=ABPR,CN=Users,DC=DRIVERS,DC=...

ADDJ user CN=ADDJ,CN=Users,DC=DRIVERS,DC=... Set-QADUser : Ambiguous identity: ALLMA.

At line:3 char:12

  • Set-QADUser <<<< $_.sAMAccountName.Replace(' ',"").Replace("\t","")
    • CategoryInfo : NotSpecified: (:) [Set-QADUser], IdentityException
    • FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.BusinessLogi c.IdentityException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets
      .SetUserCmdlet

ALLR user CN=ALLR,CN=Users,DC=DRIVERS,DC=...

ALLS user CN=ALLS,CN=Users,DC=DRIVERS,DC=...

Set-QADUser : Ambiguous identity: AMAB.

At line:3 char:12

  • Set-QADUser <<<< $_.sAMAccountName.Replace(' ',"").Replace("\t","")
    • CategoryInfo : NotSpecified: (:) [Set-QADUser], IdentityException
    • FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.BusinessLogi c.IdentityException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets
      .SetUserCmdlet
2

2 Answers

1
votes

Try looking into .Trim() rather than replace

Example:

import-csv "mycsv" | foreach-object {get-qaduser -sAMAccountName $_.sAMAccountName | set qaduser -sAMAccountName $_.sAMAccountName.Trim()}

I haven't tested but it's worth a look.

1
votes

Ignore the formatting, this is just to demonstrate that String.Trim() removes the last blank space from your string and exports it to another CSV to verify.

$array = @()
Foreach ($item in Import-Csv 'C:\CSVFileName')
{
    $obj = New-Object PsObject
    $obj | Add-Member -Name samAccountName -MemberType NoteProperty -Value $item.samAccountName.Trim() #Value is the name of the column with your samAccountNames
    $array += $obj
}
$array | Export-Csv C:\NewCSVFile.Csv -NoTypeInformation