- Now taking this scripting solution,
we're gonna elevate this to something more useful.
Now I always like to test really quick.
Now, I test it by running it here.
What I really like to do is
I want to make sure this is saved,
and I want to test it at the Console.
Notice this little asterisk over here.
That tells you that it hasn't been saved,
so you can File, Save.
Make sure that it's saved.
I'm gonna open up the Console.
I always like to test here.
Let's go into the Scripts folder,
and we'll test it real quick.
It was called DiskInfo, so let's run it.
DiskInfo, that's perfect. Running great.
This is a great solution for me except for one thing.
It's getting me this computer's disk space.
I'm sure at some point in my infamous IT career,
I'm gonna want a different computer.
Or I might want to give this script to somebody else,
and they're gonna want to use a different computer.
Maybe a couple of computers.
Well, let's show you how to make this a little bit better.
Now, what is traditional in a lot of other
scripting languages is something like this.
Well, let's go ahead and make a variable
called computername.
What we'll do is this, we'll set it equal to DC.
Then I'm gonna replace this with that variable.
Now, what this would do in other languages is,
obviously, it would make it easier
for somebody to go in and edit my script, and then ...
Wait a minute, I said edit my script.
Yeah, what could possibly go wrong?
See, this is okay but I never want anybody
ever to go in and edit my script.
So while this would work,
this is not the solution that I want.
I'm gonna show you how to make this better.
First of all, again, I'm gonna test it
just to show you that it is still working correctly.
Only now let's turn this into something
called a parameterized script.
I'm gonna show you a special keyword for scripting in here.
We're gonna type in the word param.
Then an open parenthesis.
Notice the red squiggly.
I'm not done yet.
Right after him, a closed parenthesis.
I like to indent so that it's more readable.
Now watch. I can still execute this.
Executes just fine.
Let's go do this from the Console.
Clear the screen at the Console,
and I'm going to do this.
DiskInfo, and as you can see it works just fine.
But now, watch the magic.
This is what a parameterized script does.
I'm gonna hit a dash.
It just turned that variable into a parameter.
I can put a different computer in there, and watch, see?
I got a different computer.
I could do, let's see, let's do client.
You know what, there's something that's not turned on
in the client that I'm saving for later.
So let's not do the client right now.
We'll do a different machine.
I can still do the DC, for instance.
But the premise is is that I just made a parameter.
So I could even do this.
I can go back ...
Here's how easy it is. Comma.
I want to make another parameter.
Well, it's another variable.
Let's call it, we'll just call it NotforUse
at this point, and I'll save it.
Watch how easy this is.
I'm gonna go right back to here, hit DiskInfo.
Now I'm gonna hit Tab.
There's computername. There's NotForUse.
How interesting is that?
This means that nobody needs to go in and edit my script
if I've created variables for everything
that they're gonna want to change.
Now, in this example, you can imagine
that they're probably gonna want to choose
a different drive letter,
so I would turn that into a variable as well.
But for right now, we'll keep it pretty straightforward.
Something that we like to do when we script
is we like to strongly type these variables.
In other words, specify exactly what kind
of data type they will take.
I'm gonna specify that.
Here's how we do it as a string.
I want you to watch what's going to happen
when I go to the Console.
I'm gonna do something else kind of unique.
Here I'm gonna say Get-Help on DiskInfo.
Look what Powershell is doing for me.
It's already starting to build the syntax for me.
Look, since I strongly typed
the data type that was gonna be used,
look at what it did for the computername.
It put it in there.
Now, if you remember looking at the syntax?
This shows us that it can only take one value,
but I happen to know that it can take multiple values.
I need to tell my script that it can take multiple values.
Here's how we do that.
We put in the little square brackets in there.
Now, take a look. I'm gonna save this.
I'm gonna go back and do the exact thing.
See how it built the syntax for me?
When I actually use this,
DiskInfo, computername dc comma s1,
it took multiple values.
This is a much better way to start scripting
by creating parameters.
One last thing on these parameters.
These parameters can have something
called parameter attributes.
There's a Help file if you look up
for the parameters that will give you
all of the different parameter attributes.
I'm gonna show you one of my favorites.
I don't want to have DC in here
'cause on somebody else's network,
they may never have a default computer called DC.
So I just want this to be computername,
but this command won't work unless you get a computer name.
So I want to make computername mandatory.
So what I'm going to do is type in parameter.
This is going to be a parameter attribute.
The attribute that I'm gonna use
is one called Mandatory.
Equals true. That's it.
That'll make this now mandatory.
First of all, let's just save it,
and then I'll show it to you.
We'll go out to the Console.
Again, I'm gonna run Get-Help on it.
Take a look at the syntax.
Notice how square brackets have changed.
This is now mandatory.
When I try to run this, I'm gonna clear the screen.
When I try to run DiskInfo,
if I don't put anything in, it's now prompting me.
It's dc and I strike Enter, there it goes.
I can now do DiskInfo
and I can do it this way as well.
So that's a great start to a parameterized script.
But you know, there's something else
that I think would really help you
and everybody who's gonna work with your script.
How about putting in some help?
Let's do that next.