In this chapter we are going
to cover working with bunches of objects. This allows for the management of
multiple PC's, services, or anything else to be managed across multiple
computers with a single script or command. Don refers to this a Mass Management
and it’s a very good interpretation of what you are doing.
There are a couple of
different ways to accomplish tasks such as this. The first of which is the very
basic ability to pipe objects from one cmdlet to another. For instance this example
has come up in multiple chapters, but PLEASE DO NOT RUN THIS (unless you want
to crash your computer).
Get-Service |
Stop-Service
Very simple what this command
is doing is piping a collection of objects (in this case service objects) to
the Stop-Service cmdlet and stopping them. This can be done with almost any of
the Get cmdlets. This is the preferred way to work in PowerShell. If there is a
cmdlet, use it. Don’t reinvent the wheel.
Sometimes cmdlets are not
available and you have to find other ways to gather data and perform tasks
against it. This is the case with WMI, and we covered this in more detail in
chapter 11. Don uses a reference in the book of changing network configuration
settings but a lot of the WMI objects
have methods that allow for properties to be changed. You only need to know how
to query for the information on your local as well as remote workstations and
then pipe that information to the Invoke-WmiObjects
to change the properties of that setting. This can be invaluable.
The next section is where
things start to get a little trickier, especially for those of us that have no
scripting or programming background. Enumerating of objects with the Foreach-Object cmdlet. Off to the help
file I go and after about an hour of playing in my shell, reading the help, and
web searching I think I have a better grasp of it. Basically it is a loop that
checks each object of a collection and runs a command or set of commands that
you designate. To put this as simply as possible here is an example from the help
file.
1, 2, $null, 4 | ForEach-Object {"Hello"}
What this will do is actually look at
each string or variable as an object and display "HELLO" everytime it
finds something. So what you will get as an output is: Hello, Hello, Hello,
Hello. Imagine the possibilities of what can happen with this. I am still
looking up info on this and ways that it can be used. Looking at scripts from
my co-worker and basically anything that will be performed against multiple
machines, accounts, services, etc… uses this method.
I Still have so much to learn and so
far to go, but im loving every minute of it. Have a great day and happy
powershelling.
No comments:
Post a Comment