Breaking stuff, fixing stuff and exporting results

Ok so in the next instructional video after the help info we
move on to learning the exciting ways that powershell can quickly output data
to useful formats and we learn about some neat switches. I’ve worked with
massive amounts of data before so I know how much of pain staking experience it
can be to move data around and output the data into a useful dashboard. Granted
most of we are moving in these examples isn’t what I would call huge data but
it is a pain to read in an organized/comparative fashion in powershell. However
we are given some amazing ways to process the data by creating quick html
files, CSV’s and XML files. We also learn about ways to verify that our
processes actually ran and shown a switch that prompts for conformation (this
switch is –confirm which I decided not to use in testing for some reason).
Normally if you run a command involving stopping or starting a service or some
similar action powershell just does it, doesn’t ask if you are sure you want to
proceed no matter how drastic the action is and doesn’t confirm the action was
done other than not showing an error. If you want to confirm that service has
been stopped and you didn’t use the –passthru
switch you would have to run the get-service command again to verify that the
service is stopped. So its kind of give and take far as efficiency goes on
typing out –passthru or using get-service goes.
I’m going to take this as a learn as you go and use your best judgment
type of scenario.

Ok on to notes and screen shots, this one is not quite as
substantial as the help info but it is very useful in learning to navigate the
blank piece of paper known as powershell. Also this is neat, you can launch
apps right from powershell simply by typing the name. Also prt scr and paint
leads to what seem like higher resolution images and no annoying red bars
around images.

open paint photo paint_zps1s3zel6r.jpg

Get-service  –name
bits :this shows what’s currently happening with the bits service (or whatever
service you target after the –name switch) and since we are overachievers that
want to actually learn ever thing that’s going on with this where first going
to try to mash togeather some commands and then were going to hop over to the
TechNet article about Get-service to try and understand the full syntax so that
the next time we read a TechNet article about powershell syntax maybe we can
know wtf is going on with it. As you can see in the example I tried some
different things to see what the outcome would be..

start service -passthru photo start service bits_zpspwvebnhw.jpg

So here’s the TechNet help info on get-service. At this
point it looks a little less complicated than Aramaic to me but not by much.

Parameter Set: Default

Get-Service [[-Name] <String[]> ] [-ComputerName <String[]>
] [-DependentServices] [-Exclude <String[]> ] [-Include <String[]>
] [-RequiredServices] [ <CommonParameters>]

Parameter Set: DisplayName

Get-Service -DisplayName <String[]> [-ComputerName
<String[]> ] [-DependentServices] [-Exclude <String[]> ] [-Include
<String[]> ] [-RequiredServices] [ <CommonParameters>]

Parameter Set: InputObject

Get-Service [-ComputerName <String[]> ] [-DependentServices]
[-Exclude <String[]> ] [-Include <String[]> ] [-InputObject
<ServiceController[]> ] [-RequiredServices] [ <CommonParameters>]

Ok So we see the name, that makes sense and since its listed
inside one of [ these we also know that we don’t have to give a name and if we
do that it will list all services currently running. So we could potentially
target a specific service and a specific machine. That’s helpful info and I
think we are getting somewhere with our basic understanding. The second one
differentiates the service name with the display name, I understand that in
theory but is the BITS display name not just Bits? Is there some sort of short
hand for some services where display name would come in handy? Also the third
set here input object well this is actually starting to make sense to because
its telling us that we can just type get-service and target a specific machine
using the same info as listed in the first string. Slightly redundant which
leads to a little intimidation factor for new guys such as myself but the more
im learning here the more I’m finding these strings of help to be useful in
considering full powershell syntax for more complicated commands in that you
can mix up places on some of these parameters and they will still work. So they
are also giving names to these things as if they are separate entities but I
don’t really think powershell is putting these strings into separate containers
for some reason and was you can see in this example where first I run
get-service –computername  <x> then
target a specific service using the –name switch:

 photo get-service target computer name bits_zpsaacwc0hr.jpg

 

Ok so now that that’s out of the way we can talk about all
the switches they are not telling us about in this TechNet article. Like what
to do with this data and then when we figure that out let’s try out some more
scenarios. So we’ve loaded are data into a CSV file but we don’t have anything that’s
great to read CSV files with so it looks like a notepad document and if you
have ever tried to read CSV in a notepad format you know it’s very confusing.
If this was dumping into excel or something it could be really cool. The
command we used to do that was:

get-service |export-CSV –path c:usersadministratordesktopservice.csv

Fairly simple. Ok let’s try the same thing only this time
dump it into an XML document and open it in a web browser. This has the same info
but the format is possibly somewhat easier to read buy human standards.

create an xml photo export to xml_zpshc57bxry.jpg

Lets try the same thing and this time dump it to an HTML
file and then open the html file in powershell. Ok so apparently we have to use
some different commands according the video and how the data stays cached in
what they are referring to as the pipeline is affected. Ok, lets try this, cool
it works!

Get-service |convertto-html –property name, status |
out-file  –path c:usersadministratordesktopwerbsyte.htm

drop to html open html photo results to html_zpspdep9rjy.jpg

Cool, now we know what to do with data and we’ve learned a
little about powershell syntax. Lets try to break some stuff and then restart a
server then compare the services on the working server against the server we
broke. First things first, lets use this cool Whatif command they give us and
as you can see in the screen shot this looks like it would break some shit in a
live environment:

So get-service |stop-service  -whatif

-whatif photo stop service what if_zps3vqz541l.jpg

Well if we do this on a local machine it’s going to crash
and become unresponsive so we can do that. So were going to pick on server1 and
were not sure if its going to tell us what’s going on so where going to use the
-passthru switch to make sure it tells us and it looks like its saying its
unhappy, not sure though. The full command (displayed after the results of
running the command) is:  get-service –computername
server1 | stop-service –passthru

 photo stop all services target machine_zpsoh6nagko.jpg

Well now server1 is totally unresponsive after we try to
just restart the services:

 photo restart server after service stop_zpsayqf6jfz.jpg

and we are going to have to do some thing
about that. So we google remotely restarting a machine via powershell and it
takes us to this TechNet article https://technet.microsoft.com/en-us/library/Hh849837.aspx

Easy enough, looks like its just:  restart-computer -computername

Now we are going to compare server 1 with dc 1 as mentioned
in the video and export the text to a html document so we can visually compare and
make sure that the same services are running on both machines.

Note, the passthru command does not work when trying to
restart a server. I tried it again later just to find out. It would appear that
any command will work for testing this such as ping <computername>

Ok server1 is back up and running. And some things going on
with my keyboard so we have to switch instances, fun. Why is this typing in all
caps? No idea. Google was of no avail but the scenario was easy enough to
recreate so for some reason this instance has a start button on the DC and as
you can see we have put the services running on the two machines into a separate
html files and opened them, they work great but there are some differences so
if we run into trouble we should be able to figure out why..

compairing services on two boxes via html photo compairing services on two boxes via html_zpsi0r7us5i.jpg

YAY! HAPPY FUN TIME

Thank u MSFT for test invirons, they are much fun!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: