Archive
You are currently browsing the archives for the Code category.
By Gene Laisne
Enjoyed doing the PowerShell presentation today. If anyone has any questions about PowerShell that I can help with, let me know.
On that note, the scripting guys had this post about how to explain PowerShell! (It’s like they knew)
Enjoy…
Describe Windows PowerShell to Four Types of Users
By Gene Laisne
"Make your life easy" PowerShell scripts
PowerShell
Some advanced PowerShell
By Gene Laisne
I love VBScript! I have a library of VBScript functions and scripts as far as the eye can see. I can dance and frolic (yes, I said frolic) with VBScript all day long, But... (There is always a 'but') the more I get into PowerShell, the more I like it. What would take me lines and lines in VBScript, I can do in one line of PowerShell. And as we all know, the fewer lines, the fewer bugs.
Here, I want to give people some resources that I think will be very useful for picking up PowerShell. I see PowerShell as having 4 different levels.
- Just using simple commands and getting used to piping (I know *nix folks, but a lot of Windows people aren't used to this idea of piping).
- Automating repetitive tasks with scripts.
- Writing Functions and better scripts
- Building modules and sharing code.
Admittedly, I my self am no Guru on PowerShell. I find myself somewhere at level 2-3 and only glimpsing level 4. But, where I can, I'm willing to offer a hand. Here are some resources for learning PowerShell.
Learn PowerShell before its an emergency - This is a series of beginner videos for PowerShell by "the Scripting Guy" (Microsoft's in-house script guru).
Script Center - Microsoft site for all things scripting. Chances are someone may have written something that you could use. Or, use this as an educational resource for PowerShell.
Mastering PowerShell - Free on-line book by a PowerShell MVP that looks like a great soup-to-nuts resource.
Windows PowerShell 2.0 Administrator's Pocket Consultant - Great book. This is how I got up to speed. Not an "idiots" book, but not a 'nutshell' book either, a nice in-between which is what I like for learning something new.
I have more, but I wanted to get something out there. I'll add more as I find them.
--Gene
By Gene Laisne
Recently I needed to check quickly if twelve systems were up. Rather than writeout each ping command I decided to user PowerShell to do all the heavy lifting. (I've been reading up on PowerShell recently)
What I did was write the names of the twelve systems into a text file and issue this command:
foreach ($comp in Get-Content c:\temp\hosts.txt) { ping $comp }
hosts.txt has one system listed per line and as the Get-Content parses the file, it sends each line to the ping command.
Another way would be this:
foreach ($comp in "system1","system2","system3") { ping $comp}
On the internet you'll see this example a lot of the time with something like "foreach ($num in 1,2,3,4,5)" This is nice, but numbers are not strings. If it's a string, it needs to be in quotes!
If you have a collection of systems that are setup with incrementing DNS names you could do this:
foreach ($hostNumber in 1..10) {ping oit-computer$hostNumber}
This will ping each individual system in turn starting with oit-computer1 and finishing at oit-computer10. (Be sure to use two periods (..) to indicate a span of numbers.)
But, what if you number your systems with leading zeros? Try this trick:
foreach ($hostNumber in 1..10) {$Num = $hostNumber.ToString("000"); ping OIT-Computer-$Num}
What's happening here is we have two commands between the { } separated by ';' Don't forget the ';' otherwise Powershell will error out with "Unexpected token 'ping' in expression or statement." (Each individual element on the line is a token (foreach = token, ( = token, .. = token). First we take our number ($hostNumber) and set it to have leading zeros with the member function ToString(). If you want 4 leading zeros use "0000". The result of that change (from 5 to 005 and 10 to 010) goes into the variable $Num. We then use the $Num in the command, directly, without any additional quotes or concatinations. What you get is a series of your systems pinged one at a time.
Now, go a step further if you want, try these commands:
foreach ($hostNumber in 1..255) {$Num = $hostNumber.ToString("000"); ping 10.100.25.$Num}
foreach ($hostNumber in 1..25) {$Num = $hostNumber.ToString("000"); nslookup OIT-DSKTOP$Num}
foreach ($user in "gene","matt","steve","jane") { mkdir \\server\users\$user }
foreach ($comp in "office","workstation","stevesPC") { copy c:\temp\file.txt \\$comp\c$\temp }
By Gene Laisne
Everybody needs a starting point. Now, I'm not going to teach you HOW to write scripts, think of this as more of a repository of useful scripts and script parts that I have gathered over the years.
First, I recomend using Edit Plus as your editor. It does great syntax highlighting, and it uses regular expressions in it's find, find and replace and other functions. Once you get used to it you'll wonder how you ever worked without it.
One great tool is the Microsoft script repository. Lots of little scripts to get you going. http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true. Also, you can look around the scriptcenter for some help.
For me, when I need to make a script I try to do it in pieces. I've built some functions and classes along with script templates where all I have to do is drop a few things in here or there and assemble the script. Makes things much easier.
Here are some of my basics. I start with a vbscript template. This gets me started. There are a collection of variables, objects and constants that I use on a reglar basis that are constant through out all my scripts. I recomend using a naming convention and keeping it as constant as possible. For me constants are in all caps, and all of my objects start with 'obj' as you'll see in the file. Also, my scripts have a header so I can keep track of changes that may have been made.
Help!
The next thing I do is drop in my help() function. This function is designed to require switches and if a user enters "/?", "-help", "--help" or any of a few other "help switches" they get the help output just like they were using a regular dos program.
There are two parts to this function. First, there is the help() function itself, which will need to be editied for you collection of switches, and then there is the parsing section of this template. The help() function, I always put at the end of the script, in fact, my scripts are all organized like this:
- Header
- Global variables (I know, you're not supposed to use global variables, but this is scripting... give me a break. Besides, if you careful, and know what you're doing this is fine.)
- Script
- Classes
- Functions
Now, the parsing portion of the scripts is setup to go through each argument to the script. The arguments are in an array so if you using a script that is exactly like the dos command 'copy" then you can make the first argument 'source' and the second argument 'destination.' If, on the other hand, you were building a script like 'gpresult,' you would use the current argument to get the current switch, and the next argument (current index + 1) to specify the parameter.
Example:
objArgs is the argument object, this has all the arguments.
argCounter specifies the current index in the arguments.
So, "objArgs(argCounter)" references the current argument.
if "objArgs(argCounter)" is the current argument, then "objArgs(argCounter + 1) is the next argument.
So, if we wanted to use "/s <system name>" as an argument for our script, then in our select statement, we would have:
Select Case objArgs(argCounter)
Case "/s"
strSystem = objArgs(argCounter + 1)
End Select
So, that's the start of any good script. See the attached files.
funchelpvbs
vbscriptvbs