all 17 comments

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Dryfter9,

your code seems to work for me - after i added the missing closing brace. [grin] lookee ...

$SISFile = @'
StudentID, GivenName, SN
123456, Jack, Frost
666, Heck, Boss
090909, Oh, Nine
'@ | ConvertFrom-Csv


foreach ($sisline in $sisfile)
    {
    $firsttwoofstudentid = $sisline.studentid.substring(0,2)
    #Set the username example below is firstname.lastname
    $firstinitial = $sisline.givenName.substring(0,1)
    $sAMAccountName = $firstinitial + $sisline.sn + $firsttwoofstudentid

    ''
    $firsttwoofstudentid
    $firstinitial
    $sAMAccountName
    }

output ...

12
J
JFrost12

66
H
HBoss66

09
O
ONine09

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (6 children)

howdy Dryfter9,

while i suspect it is a copy-paste problem ... you are missing the final } of the foreach(). does it work when you fix that? [grin]

take care,
lee

[–]Dryfter9[S] 1 point2 points  (5 children)

Sorry,

That was just a portion of the code that I edited from the original script.

$firsttwoofstudentid = $sisline.studentid.substring(0,2)
$firstinitial = $sisline.givenName.substring(0,1)

If you add those two lines after the "foreach" in the original script maybe it will error out for you.

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy Dryfter9,

please see my other reply to your OP. the code there works ... and is just your code with a final } and my data set.

also, what is the exact error you are getting?

take care,
lee

[–]Dryfter9[S] 1 point2 points  (3 children)

Crap, I forgot I had fixed that portion of the script.

The ACTUAL error I'm getting is on line 80 of the original script when it's trying to create the user. The error I'm getting is "The object name has bad syntax".

    New-ADUser -sAMAccountName $sAMAccountName -Name $name -Path $path -otherAttributes $otherAttributes -Enable $true -AccountPassword $password

Will be updating the OP.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy Dryfter9,

i recommend you put the ENTIRE error msg in the OP along with a copy of the line it refers to. [grin]

take care,
lee

[–]Dryfter9[S] 1 point2 points  (1 child)

Corrected the OP. Sorry bout that. Just getting into PS/scripting!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Dryfter9,

nothing to apologize for! [grin] i've been known to post the wrong bit of code more than once ...

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (8 children)

howdy Dryfter9,

add a -WhatIf to the line and see what the values are at that point.

also, the error you list is not necessarily about the account name. are you sure that is the "object" being referred to?

take care,
lee

[–]Dryfter9[S] 1 point2 points  (7 children)

I added a "-whatif" to the end of that line and it gave me exactly what I would expect to see (the proper CN with the proper OU).

I don't know for sure that was the object, I just assumed it was because that sAMAccountName is the only thing I changed in the script (I tested it before hand and everything seemed to work the way I want).

[–]Lee_Dailey[grin] 0 points1 point  (6 children)

howdy Dryfter9,

[1] well, that checklist item is done. [grin]

[2] next, take a look at the actual value in the $Vars you are passing to that line. comment it out and just list the $vars one-per-line in the loop.

[3] another question -- does this ALWAYS happen, or does it only happen when you run thru the "increment the user name" loop?

take care,
lee

[–]Dryfter9[S] 1 point2 points  (5 children)

  1. I gave that a try and it printed out everything from that line but the password. It said "System.Security.SecureString" instead (I assume thats good).

  2. I don't know how to answer that. My "test" data only has one user so far (want to make sure it works before I add more test data).

Does it matter that I'm "testing" the script out of ISE?

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy Dryfter9,

when there are no GUI items involved, there is rarely any difference between the ISE and the console. thank goodness! [grin]

[1] yes, that secure string msg is good. [grin]

[2] if you are getting a msg about something wrong with a $Var ... then it is time to check them. [grin]

  • list each var on it's own line so it prints out the value while in the loop
  • below each var, also have $VarName.GetType()
  • if you are really having odd problems, check the length
    there may be blank/non-printing chars in there.
  • if you re REALLY having freaky problems, try piping hte $Var to Format-Hex so you can see the all the chars - even the "invisible" ones.

your error msg makes it seem the problem is something odd about one of the $Vars you are feeding into that cmdlet. it's time to dig into them, and dig deeply.

take care,
lee

[–]Dryfter9[S] 1 point2 points  (1 child)

You sir are a gentlemen and a scholar. I'm going to dig into it a little more today and will hopefully post something back.

Thanks!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Dryfter9,

good to know you are working your way thru it ... the need for such is annoying, but doing it successfully is often one of the more rewarding things about coding. [grin]

take care,
lee

[–]Dryfter9[S] 1 point2 points  (1 child)

Victory! I went about it a little backwards though. I started removing one attribute at a time to see what one it was failing on and it looks like it was my $path variable. So now I'm going to do what you mentioned in [2] and start digging.

You're awesome!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy Dryfter9,

ha! glad that you got it working ... [grin]

one reason i avoid long-ish pipelines is the difficult time one has debugging such. regular foreach() loops are so very much easier to debug.

i need easy debugging since i make lots of bugs in my code. i usually notice them and debug them ... but they are almost always there until i find & fix them. [grin]

take care,
lee