Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

TheIncredibleAf (Programmer)
25 Jan 11 6:32
Hi, I've just joined here because it looks like a good place to troubleshoot PowerShell (which I've just started to learn) and I'll be doing some Android programming at some point so I'm sure I'll be needing all you guys help.

Ok...My problem is that the code below brings about the dreaded cannot index into null array error when using the $ListOfUsers variable.

Does anyone have any ideas how I resolve this or rewrite the code to work better?

Cheers.

NOTE: All variable instances not initialised have been done so in the code elsewhere

CODE

# Store the list of users due to expire
$ListOfUsers[0]
$Count = -1
# If the user's account is due or almost due to expire then capture details of account into an array
if (($DaysLeft -eq $NumOfDays) -or ($DaysLeft -le $NumOfDays) -and ($DaysLeft -ge 0) -or ($DaysLeft -le 0)) {
            
  $ListOfUsers[$Count++] = $Users.Name
  $ListOfUsers[$Count++] = $Users.Email
  $ListOfUsers[$Count++] = $ExpiredDate
  $ListOfUsers[$Count++] = $DaysLeft
}
crobin1 (MIS)
25 Jan 11 12:52
What is the standalone line
$ListOfUsers[0]
supposed to accomplish?

Your If statement is also too complex. Based on this code it should just read:
If ($DaysLeft -le $NumOfDays)

-le stands for "Less than or Equal", so that covers your first clause testing just for equal. Your last two clauses comparing $DaysLeft to 0 mean $DaysLeft can be any number including 0. Unless there was something else you were trying to test for those last two clauses aren't needed.
TheIncredibleAf (Programmer)
25 Jan 11 14:27
$ListOfUsers[0] was my thinking of how you initialise an array

I've since learned elsewhere that this is accomplished with
$ListOfUsers = @( )

But now when I reference the array, I get the error:
Array assignment failed because index '0' was out of range.

Basically, how do I create an array that I can then store information in like the following

$ListOfUsers[0] = "Peter"
$ListOfUsers[1] = "Michael"
$ListOfUsers[2] = "John"

Thanks.
crobin1 (MIS)
25 Jan 11 18:09
This is probably your answer: http://www.leedesmond.com/weblog/?p=183

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close