So another approach is to actually want to execute some kind of script.
So instead of using static commands such as Get‑ComputerInfo or hostname,
maybe we want to execute a script into a specific set of machines such as
our Active Directory server and our member servers.
So I'm going to press the Start button, and type the run
command, and then execute the run.
And then I'm going to use my IP address of the AD server, so 10.0.0.5.
Just click OK, and this will launch into a series of
shared folders on that machine.
I'm going to go into my Scripts folder, and in here, I
have a series of PowerShell scripts.
What I'm going to do is actually just go and right‑click here on
Report, and I'm just going to copy that, and then I'm going to browse
to my C: drive here, and I'll just go into my Code folder, and I'll
just paste that PowerShell file.
So this is a Report PowerShell script, which we'll look at in a second,
but I just wanted to copy it locally so we could try and execute this
against another machine, so I'm going to minimize now.
Now what we can do is take the same command that we
had previously. And, for example, where we have this section here,
which is hostname,
I'm now deleting it, ‑ScriptBlock, etc, we can actually change that to be
‑FilePath and then point it to the file path that we want to use.
So a specific report, PS1 file or whatever the script may be.
Now, what I want to do here is not actually execute it
against all of the machines for now.
So what we'll do is a 10.0.0.5, so just against the Active Directory
server. And then in my file path, I could put the file path.
So, if we go back to my run option here,
you can see it's in my C:\Code directory.
So I minimize here. I'm going to say
"C:\Code\Report.ps1", and I have my kind of path.
Now, what we can do here is I can execute this here. Now you'll notice it
comes back and says the value of FilePath needs to have a .ps1 extension, and
that's okay, so let's just delete these extra bits.
This is where our syntax differs between the different types of things.
So, now I've changed it, Invoke‑Command, this single machine
with the credentials we have in that PS1 file.
So I'm now executing it.
It comes back and says, oh, hold on a minute. A file already
exists, and that's okay because this report file,
which we'll look at in a second, is trying to generate a specific log.
So let me just click the Start button again,
go to my Run command. This time, we'll actually go through and
use that specific path, \\10.0.0.5\Devices, and we'll open
that, and you'll see, sure enough, there's a text file.
So I'm actually going to delete this, and we'll leave this window
open and go back to my PowerShell window, and then we'll rerun that
same command like so, and sure enough, it now says it's created a specific file.
So if we go back to my Devices folder, you'll see there's a Report file.
If I click Open,
we'll just maximize this. You'll see it gives me some
information around the Windows desktop,
the manufacturer, and then some details about the operating system.
So, how does it do that?
Well,
let's close this down, and this time, we'll go and open
that specific script file that we created.
So I'm going to right‑click here, and I'm going to say
Open with Code, which is Visual Studio Code. And sure
enough, my Report file is now open.
This is my PowerShell script. So you can see it has a location set
of where we wish to save, and then I'm using some variable
information, so this is a standard PowerShell command, so env, so
$env is for current environment.
And then I'm using a property called COMPUTERNAME. I'm then
using a default PowerShell cmdlet called Get‑CimInstance to get
information about the desktop, about the actual manufacturer,
operating system information.
And then, if we go to the last bit here,
I'm saying go to this location and basically create a report log file
that has the populated variable information broken down by these kind
of topic areas, so desktop details, manufacturer details,
operating system details.
So fairly straightforward.
So, let's go back and see how we can run that against
multiple machines instead of just the one.
So let me clear my section.
Now, of course, if we go back to my command,
you can see that I've got my PowerShell script to run locally.
I've got my $creds. And all I want to do here is
just change this to be $computers.
Before we execute, I'm going to go back to my Devices directory, and I'm
actually just going to delete the file because I don't want to have an
error again. Click back here, and then what we'll do is go right to the
end here and just execute. Now this is going to go ahead and try and run
them. Now, of course, well, we can see we've got some errors to do with
access denied.
So what's happening here is it's trying ‑‑‑ I'm going to scroll all the way
up. I'm going to keep scrolling back to the top here.
It's trying to write. You can see access to the path it says is denied,
so it's definitely connecting to the other machines, so TRAININGSRV02, which is
one of my member servers, and it's trying to write the value to it, and then
it's failing with some various permission issues.
So if we just go back here to my folder,
my Devices, sure enough, it only created the very first one.
So why does it do that?
Well,
it's because it's to do with permissions when it's been executed from a remote machine.