Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Limit String to Specific Length

Posted on 2014-10-13
2
Medium Priority
?
2,200 Views
Last Modified: 2014-10-13
I'm using a PowerShell script to create new users in AD from a CSV file. The script I have I've used before, and I'm sure it used to work without errors. However when I run it now, I get an error when trying to limit the samAccount name to 20 characters max. It actually works fine for names over 20 characters, it checks the length, and crops it to 20. The errors happens if the name is under 20 characters. The code I'm using is:
        $sam = $_.GivenName.ToLower() + "." + $lastname.ToLower()
        $length = 20
        If ($sam -gt 20) 
        {
        $sam=$sam.Substring(0,$length) 
        }

Open in new window

The error I get is:
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string. Parameter name: length"

Not sure why it's erroring on characters under 20, I'm thinking it should only run if $sam is greater than 20, if not, it should skip on to the next function. How can I get rid of this error for names under 20 charactors?
0
Comment
Question by:bjblackmore
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40378357
Use the following condition to check the length..  If ($sam -gt 20) will not check the length of the string..
If ($sam.Length -gt 20)

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40378390
$sam = $_.GivenName.ToLower() + "." + $lastname.ToLower()
$length = 20
If ($sam.length -ge 20) 
{
$sam=$sam.Substring(0,$length) 
}

Open in new window


Edit:
Disregard I see Subsun already responded
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question