0
votes

I have a script which i use to create bulk users from a csv file which works fine.

Import-Csv e:\temp\newemps.csv | %{
    $ou = $_.ou
    $firstname = $_.first
    $lastName = $_.last
    $accountName = $("{0}{1}" -f $firstname.Substring(0,1),$lastName).ToLower()
    $description = $_.desc
    $password = "Welcome1"
    $name = "$firstName $lastName"
    New-AdUser -SamAccountName $accountName -GivenName $firstName -UserPrincipalName "[email protected]" -Surname $lastName -DisplayName $name  -Name $name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -Enabled $true -Path $ou -Description $description -ChangePasswordAtLogon:$False

    If ($_.Group -ne ""){
        Add-adgroupmember -identity $_.group  -members $accountName
    }

    If ($_.email -eq "y"){
        Enable-Mailbox -Identity $accountName -Alias $accountName
        Set-Mailbox $accountName -MaxSendSize 10mb -MaxReceiveSize 10mb
        Get-CasMailbox $accountName -OwaEnabled:$false -ActiveSyncEnabled:$false
    }
}

I was trying modify this script so that i could create some generic accounts that would not follow our typical convention. The input is a here-string as supposed to a csv as the only unique item is an Airport code. I have shortened the here-string for brevity.

$bases = @"
YAB
YEK
YYH
YHI
"@

$bases.Split("`n") | %{    
    $ou = "CN=Users,DC=BA,DC=NET"
    $firstname = "$_".ToString()
    $lastName = "Counter"
    $accountName =  "$_" + "Counter"
    $description = "Base Front Counter"
    $password = "Welcome1"
    $name = "$firstName $lastName"
    New-AdUser -SamAccountName $accountName -GivenName $firstName -UserPrincipalName "[email protected]" -Surname $lastName -DisplayName $name  -Name $name -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -Enabled $true -Path $ou -Description $description -ChangePasswordAtLogon:$False
}

There is something about using a here-string that I am not accounting for. The only account it successfully creates is the one for YHI (The last one of the here-string). For all others it gives New-AdUser : The object name has bad syntax. Internet research shows many errors for csv-imports where the data has whitespace and other issues there but im not sure what the issue is here.

In the end I just made a csv file instead of using the here-string but I would like to know what i was doing wrong.

1
Why use a here string? Just make an array of strings... $bases=("YAB","YEK","YYH","YHI") and then $bases | ForEach{...TheMadTechnician
It was a one off. I copied the data from a list which already had the newlines. Here-string didnt require me to format the list. I just had to paste in in place. And no, there wasnt any whitespaceMatt
I know you said there is no whitespace, but I kind of wonder if there are hidden characters or some such perhaps being copied from the other file. Unicode or whatnot. If it were me I'd take your $Bases=Here-String and do $bases.split("n")[0].length and see if it comes back longer than 3 characters. Perhaps there is a CR along with the LF being copied? There's a lot unknown. Maybe do a Write-Host ">$($bases.split("n")[0])<" to make sure it appears as >YAB< like it should.TheMadTechnician
I checked for spaces so it was premature to assume there was not whitespace. The length ended up being 4. The extra character is the carridge return. $bases.Split("`n")[0].ToCharArray() | %{ [int][char]$_}Matt

1 Answers

1
votes

This worked for me. got rid of the null values and the new line values and just gave me each string value from each line. Seams there may have been some white space or some other characters that interfere if you just do split "`n"

$test = @"
user1
user2
user3
"@
$test.split(“`r`n”) | ForEach-Object {if($_){get-aduser $_}}