- Using all of the information and knowledge
that we've accomplished together in this course,
it's time to build our first script together.
And this is one of the interesting things,
is why we always start in the console.
This is where I solve my problem
and then I start to turn it into a script.
So let's do something really straightforward.
I want to get some event log information,
so I'll use Get-eventlog and LogName,
I want to get it from the system
and I want just the Newest 5, and an EntryType of error,
believe there'll be some of those
and I like to check as I'm going along
to see what kind of information I'm getting.
You know, this is great, I like this,
but I really only want a certain amount of information.
I want the Index, Source, and the Message.
Now, if I'm not sure of the exact property names
and I need to warn you, these column names
may not be the exact property names.
Always pipe to Get-Member to take a look.
In this case I believe they are, so let's go ahead.
And we'll go, I want to select these by property
and now I forgot exactly what I wanted to select,
so let's take a look again.
There's the pipeline and I wanted, oh yeah,
Index, Source and Message,
so we'll just do Select-Object
by Property Index, Source and Message.
So already, I'm starting to build out
a solution to a problem.
Here's the problem I'm having is that,
I want to report of all of the latest errors
that are occurring on several different computers.
So,I also want that report to be in an HTML format
so I'll convertTo-HTML and I want it stored on my C-drive,
so out-file and I want it called C:\error.htm.
And I want to get it from all of the computers so,
let me go back here add in -computername,
and in this case, DC, S1 and S2.
And again, I'll check to make sure it works.
It's going to take it a moment to grab
that information from those other computers,
and as soon as it completes,
I'm going to open up that HTM file and verify
that it is giving me the information that I want it to.
Now again, as when we were working with the HTML reporting,
I can make this much prettier,
but this is good enough
to get me started with on my solution.
Now, I'd love to have this in a script
so that I could run it through the Windows scheduler.
So, let's turn it into a script.
I'm going to show you how easy this can be.
I'm going to hit the Up Arrow Key,
I'm going to copy this, and right-click,
open up Notepad, and paste.
Now, when you paste into Notepad,
one of the things you're going to have to do,
is clean this up so that it still works correctly.
We're going to get a chance
to test it here in just a minute.
It doesn't look all that impressive,
because I'm just pasting it into Notepad,
but this is one way to make a script.
So, as I scroll across, everything is together, once again,
and looking correct.
I need to save this as a Powershell script file,
so I'm going to Save As,
and I like to especially when I'm testing a script,
I like to have them in a special folder,
so let's make a new folder called, oddly enough, Scripts.
So, I'll make Scripts.
I'll go into that folder and let's call this Error.PS1.
PS1 is the script format
or the extension that you want to use,
and now we can try to run it.
Now there's a couple of things about running scripts
with Powershell, and this is done as,
well to protect us from past problems.
Let me give you one example.
You might remember, the DIR rootkit
that had occurred many, many years ago.
It replaced your original DIR Command in the path,
the Windows path, so when you ran DIR,
you were running the rootkit.
To solve this problem, with Powershell scripts,
what they do is, you must put in the complete path
to the script, such as this, in order to run it.
There.
Or, if you're in the same folder you can reference it,
but you can't just type Error.PS1.
You've got to give me the path to it.
In this case, what I'm going to do is,
is I'm going to go to Scripts
and dot, backslash, E-R, I'm going to hit the tab key,
you'll see it complete it, and I'm going to try to run it.
Now if you remember, I don't have my script execution
policy set correctly, and this is the error message,
that you will receive, if you don't.
Notice it says, it can't be loaded
because the running of scripts is disabled on this system.
This is a common error that people run into
when they start working with Powershell.
It does tell you to go look at the about_Execution_Policies.
How I'm going to solve this is, as we did earlier,
I'm going to set my set my Execution Policy to remotesigned.
Say, Yes, clear the screen.
Let's try this again.
Now remember, this doesn't have any screen output
cause I had it saving information to a file,
so it's running right now.
What I'm going to do is, just to prove that it's working,
let's open it up again in Notepad,
and let's make a change to it,
just so you can see that it's,
oh, there's the HTM (laughs)
that it made, so that kind of proved
that it was working,
but let's go ahead and open this up
so that you can see,
oh no, no, no, I don't want that!
I forgot what I called the script file.
What did I call it?
Oh, Error.PS1.
Ok, Notepad to Error.PS1.
There we go.
So I'm going to make a change here,
and let's just reduce this down to one computer.
We don't need to see a lot of information.
I just want to prove to you that it did work.
The concept, of course, is solve your problem
at the console and then copy that into a file.
It's a text file named with a .PS1.
It's that simple.
And so, once again, I'm going to run this, Error.PS1,
and you can see that it's working just fine.
Now this is our first script together,
and what I really wanted you to get was
we solve our problem in the console,
and then we try to make a script out of it from there.
Also, is to make sure you have the right execution policy.
Now, there's something else I want to take a look at,
another way to store data,
and then I'll show you
a better way to make scripts.