Solved

Limit String to Specific Length

Posted on 2014-10-13
2
1,360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

749 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