Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!

Swink posted:

How can I validate user input? I have a script that creates AD users, and I need to specify the users' location. There are four options, how can I ensure I dont misspell the city when I'm typing it in? Is there any way I could choose from a list of options rather than having to type?

With parameters, you couldt use a validateset:
code:
param
    (
    [Parameter(Mandatory=$True)]
    [validateset("Mars","Mordor","Sodom","Gomorra")]
    [string]$City
    )
If you use interactive input, you could either check the statement with if statements:
code:
if ("Mars","Mordor","Sodom","Gomorra" -eq $city) {write-host "Wow! you can type correctly!"}
For this to validate, you need the list of correct cities on the left hand side, making the statement: "If any of these match". Note also that this isn't case sensitive. You could use a switch statement:
code:
$Choice = Read-Host "Select 1 for Mars, 2 for Mordor, 3 for Sodom, 4 for Gomorra"
Switch ($Choice)
     {
     1 {$City = "Mars"}
     2 {$City = "Mordor"}
     3 {$City = "Sodom"}
     4 {$City = "Gomorra"}
     }
VVV Indeed, you can do fun things with validation. You could ensure the $Max is always bigger than $Min by using a validate expression, for instance. VVV

Jelmylicious fucked around with this message at 06:32 on Jun 27, 2012

Adbot
ADBOT LOVES YOU

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
Just to explain what Mario is saying here: Single quotes take everything as written, while double quotes interpret everything written. Backticks make thing that would be interpreted literal again. To illustrate:
code:
PS C:\> $var = 5
PS C:\> "$var"
5
PS C:\> '$var'
$var
PS C:\> "`$var"
$var
PS C:\> '`$var'
`$var
Notice in the last one that the backtick is also simply written out.

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
I believe that if you have an array and the next command accepts an array as input, it will just pass it as one thing. Not sure though.

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
Didn't really look over your code, but your += is definitely in the wrong place. The following statements are the same:
code:
$a = $a + $b 
$a += $b
+= is just a shortcut for: add something to my variable and make that the new variable.

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
Then just sort the list you have:
code:
$users | sort surname | select-object givenname, surname
Or, make your list sorted after you filled it, so it will stay sorted:

code:
$users = $users | sort surname

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
An easy place to start, to get a good feel for how powershell starts, is Don's youtube channel: http://www.youtube.com/user/powershelldon
This is from the same author as the month of lunches book. This might help you get a good grip on the quirks that are specific to powershell. If you only watch one video, use this deep-dive (warning, it is 4 hours long) https://www.youtube.com/watch?v=-Ya1dQ1Igkc

Adbot
ADBOT LOVES YOU

Jelmylicious
Dec 6, 2007
Buy Dr. Quack's miracle juice! Now with patented H-twenty!
If you just put c:\script.ps1 in the task scheduler, it will execute it with the default associationg for ps1. So, your server will be very happy to oblige and just open notepad for you. Make sure you add powershell -file 'c:\script.ps1' to have it actually execute. Also, don't forget to put plenty of logging in the script, since you won't be monitoring it.

See this for an example task: http://blogs.technet.com/b/heyscrip...ell-script.aspx

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply