Sie sind auf Seite 1von 37

Scripting

Week 1-2 Topics


Nirmal Anam
Matthew Cubis

Topics

What is Scripting (slide 3)


Scripting in this course (slide 4)
Virtualization and Virtual Machine (slide 5)
What is Powershell (slide 7)
Installing Powershell (slide 11)
CMDLET structure (slide 15)
Commands in Powershell (slide 17)
Wildcard Characters * ? (Slide 19)
Get-Command Information (Slide 22)
Get-Help Information (Slide 27)
Aliases (Slide 35)

What is Scripting
Automating things
Making life easier by automating repetitive stuff
Helping us do something we cannot through any other way
Helping us make the transition of data from one system to another
with ease
An alternative way of communicating with a certain system
An intermediary communicator to help us work with more than one
system

Scripting in this course


Scripting in this course will be covered using:
Windows Powershell
Windows Server
SQL Server

Virtualization and Virtual Machine


Instructor will now explain what is virtualisation and how to use the
virtual machine.
This part is only used in this course. You will not be assessed on these
virtualisation concepts.
However, during assessments you will be required to have the
knowledge to create and maintain your virtual machine such that you
are able to perform all the practical tasks required for the scripting
course.

Virtualization and Virtual Machine Task 1 (Do it


yourself)
Creating a virtual machine and setting up our environment

What is Powershell
Command Shell that runs on Microsoft Windows Operating Systems.
A tool for administering Windows OS and applications (both
Microsoft and third party applications)

What is Powershell
Windows has many alternatives to powershell which all fall short when it
comes to accessing, manipulating, managing and maintaining Windows OS
components and applications. These alternatives include:
Windows already had Graphical user interface and CMD.exe command line shell
Windows also introduced WSH Windows Script Host which supports VBScript and
Jscript.
Several other WSH Compatible scripting languages like Perl, Rexx, Python also exist

But Microsoft wanted a shell that is more powerful, hence Powershell.


Makes sense?
Actually, Various other reasons on why powershell is powerful will be
revealed (or experienced) as we go through the course

What is Powershell
Alright, here are a few quick reasons you can tell your friends when
you are arguing about powershell:
Most recent Microsoft operating systems and applications actually use
Powershell behind the scenes to perform tasks even when the user uses GUI
on the front end.
Standardised syntax structure while talking to OS and a wide range of
applications
Powershell treats everything as objects
Powershell comes with its own scripting language to perform complex tasks
Greater support when it comes to accessing OS components and applications

What is Powershell
Windows OS comes with Powershell Console and ISE
Powershell comes as x86 or x64 bit editions (32 bit, 64 bit)
On a 32 bit OS you will have only the 32 bit version of powershell console
and ISE named as Windows Powershell and Windows Powershell ISE
respectively.
On a 64 bit OS you will have both the 32 bit and 64 bit version of
powershell console and ISE. The 32 bit versions of Console and ISE are
named as Windows Powershell (X86) and Windows Powershell ISE
(X86) resepectively.
The 32 bit version of Powershell is used to talk to a 32 bit application
which is not compatible with the 64 bit version powershell.

Installing Powershell
As mentioned before Windows Client and Server OS come with Powershell pre installed.
To get powershell or upgrade powershell to a later version you need to update Windows.
To manually upgrade powershell download and install:
Microsoft management framework (This requires Microsoft .NET Framework)

For Powershell 3 you will need Microsoft management framework 3.0 (which requires at least
.NET framework 4.0)
For Powershell 4 you will need Microsoft management framework 4.0 (which requires at least
.NET framework 4.5)
For further details on the requirements check Microsoft Website.
This course will be using Powershell Version 5. However, almost everything on this course should
still be possible with Powershell as low as Version 3
Powershell Versions 3 and 4 are backwards compatible with Version 2. So You can install and use
version 2 alongside version 3 or 4. However, Version 4 installation will replace version 3. Version 1
is also replaced by any of the other versions, so it cannot exist alongside other installations like
Version 2.

Installing Powershell
To check the version of the powershell:

Get-Host
Host
$host
$host.version
$PSVersionTable
$PSVersionTable.Psversion
Get-Host | Select-Object version

The above examples demonstrate Cmdlets, variables, piping (Passing output of


one command as input to the next command).

Working with CMDLETS - Task 2 (work along)

Get-Host

Clear-Host

Write-Host

Read-Host

Get-ChildItem (Also dir, ls)

Set-Location (Also Cd)

Get-ExecutionPolicy

Set-ExecutionPolicy

Get-Help

Update-Help

Save-Help

Get-Command

Get-Date

Get-Process

Get-Service

Start-Process

First Script - Task 3 (DIY)


Write a script in notepad and execute it in the powershell console.
This script task will be based on the cmdlets learnt so far.

CMDLET Structure
cmdlet is the native powershell command. It is pronounced
Command-let.
While searching on google for powershell related information using
the word cmdlet instead of command will actually give you more
related resources.

CMDLET Structure
Example:

Get-Process name explorer,powershell* -computername localhost

Get-Process (cmdlet). Notice how cmdlets are always having a verb-noun format. The verb part
refers to the action you want to perform write, read, get etc; and the second part refers to the
object you want to perform the action upon/with process, service, host etc; Also note how the
noun forms are singular (not processes, services). This description is true for most of the cmdlets
which makes it easier to find cmdlets when you need to.
-name (parameter name). Parameter names start with . You can abbreviate the parameter
names as long as they are distinct enough for powershell to identify them. Sometimes you dont
have to mention
Explorer,powershell* (Arguments). We are passing two values explorer and powershell* as
arguments to the parameter computername Since these two values are single words we didnt
have to enclose them in quotation marks, but if a value has multiple words then we need to
enclose it into quotation marks (For example : -computername Room1 ServerA). Note that if you
want to pass multiple values for a parameter then you need to use a comma to separate them.
There should be a mandatory space between commandlet and parameter name, between
parameter name and argument, argument and the next parameter.

Commands in Powershell
There are different types of commands (loosely defined for the purpose of this
course) cmdlet, function, workflow, application which are all generally referred
to as commands.
Cmdlets are native to powershell, so they run from within powershell.
We will see what functions and workflows are as we progress further in this
course. To be brief, we can create function/workflows with a certain name in
order to perform certain tasks. Once they are created they can be used by using
their respective names we provided them during the time of creation.
An application is any executables, command line utilities that can be run in
powershell (For example, Ping, ipconfig, notepad, calc). We can type these
directly into powershell console and powershell will execute them. For utilities
which are generally from CMD.EXE like ping, ipconfig etc; powershell opens the
CMD.EXE on the background to run them.

Commands in Powershell

Wildcard Characters * ?
Wildcard characters available in powershell are * and ?
Wildcard characters are used as a matching mechanism
* matches with any 0 or more characters
? Matches with any 1 character

Wildcard Characters * ?
Consider the following main list of words Cat, Mat, Bat, Rat,
Mt, M, Hat, Sat, At.
*at means match with any word that begins with anything and ends
with at. So from the above main list it will match with Cat, Mat, Bat,
Rat, Hat, Sat, At. Note that it will also match with the word AT
because * matches with 0 or more characters.
M* means match with any word that begins with M and then has
anything after that. So it will match with Mat, Mt, M.

Wildcard Characters * ?
Consider the following main list of words Cat, Mat, Bat, Rat,
Mt, M, Hat, Sat, At.
M? means match with any word that begins with M and then has
any other 1 character. Mat begins with M but has two characters after
that so it doesnt match with M?, Mt begins with M and has only 1
other character so it matches with M?. Note that the word M doesnt
match as well because it begins with M but it doesnt have 1 extra
character since the pattern is looking for M?. So the only word that
will match with M? is Mt.

Get-Command Information
Get-command is useful to list the commands in powershell.
Very often we might need to check for applications (.exe
executables) that are possible to be run via powershell,
check the different cmdlets that are available to work with
process or service or any other object, check which cmdlet
come as part of a certain module etc. and get-command will
help us in all these situations
We can also use wild card characters with Get-Command

Get-command with wildcard characters


We know that cmdlets are usually of the form verb-noun.
So when we are working with process object we know that we have
get-process. To know about the other options with respect to a
process object we can use get-command with wild card characters.
Get-command *-process
list us cmdlets.
Cmdlets that match *-process, so essentially any cmdlet that ends with
process.

Get-command with wildcard characters

Get-command with wildcard characters


Since we are looking for the word process in the noun part of the
cmdlet we can also search for cmdlets by using the noun parameter

Get-command with wildcard characters


Similarly we can also use the verb parameter for the get-command

Get-Help Information
Run Update-Help to update your help information (These are files in your
system 32 directory. So you will need to run powershell as an
administrator).
Once the help files are updated Get-Help can be used with a lot of
additional options like full, detailed, examples, online. These options give
specific in depth information about a cmdlet.
Update-Help will update the help information using internet as the source.
If a computer is not connected to internet then Save-help (on a computer
with internet) can be used to save the help files locally or in a shared
directory. This can then be used to update the computer with no internet
access by using Get-Help with additional parameters which point the
source to the shared directory.

Get-Help Information
Get-Help will allow us find all the help files available in powershell.
Help files are available for cmdlets
Help files are also available for powershell concepts like variables, wildcard
characters etc. These help file names generally start with about in the listing.
We can use Wild Card Characters with Get-Help to search through the available
help files
Get-Help *-Process
will list us all the help files that end with process

Get-Help *variable*
Will list all the help files which have variable in the name
If a search matches multiple file names, then get-help will just list all the matching help files
If a search matches with only one help file name then get-help will display the

Get-Help Information

Get-Help Information

Get-Help Information
It is very essential to be able to read the Get-Help syntax information

Get-Help Information
Looking at the syntax section we can see that there are 6 different parameter sets available for
Get-Process. So Get-Process cmdlet can be executed by using any one of these parameter sets.
We cannot use parameters from two different sets.
We can see that the structure of the cmdlet is just as we discussed before in the cmdlet structure
section. There is a cmdlet followed by parameter name, argument data type (or value expected
for the parameter) followed by other parameters.
Note that after a parameter name there are words in angular brackets <> like string, int32. These
are referred to as argument data types. These define what type of a value can be given to a
parameter. For parameters with int32 as the argument data type the value should be a number,
string means the parameter accepts a character, word or a sentence which can be made up by
alphabets, numbers and even special characters. For example the value for parameter id can be
1234 etc.
Note the square brackets [] at the end of the argument type <string[]> or <int32[]>. This means
that parameter will accept multiple comma separated values. This is why we can give multiple
values to a parameter sometimes. If you dont have these [] at the end of the argument data type
then we can only pass one value to that parameter. Example for passing multiple values: name
explorer, PowerShell etc.

Get-Help Information
Note the square brackets on the outside of a parameter name, argument
type set. Example: [-computername <string[]>]. This tells us that the
parameter is an optional parameter. We can run the cmdlet with or
without providing the parameter and its value.
There is another set of square bracket just around the parameter names.
Example: [[-name] <string[]>]. We are referring to the square bracket
around the parameter [-name]. This tells us that the parameter is a
positional parameter which means that a value can be given for this
parameter without specifying the parameter name. As long as the values
you are giving as arguments to this parameter are placed in the exact same
position as shown in the syntax, powershell will pass these values to the
respective parameter.

Get-Help Information
EXAMPLES:
Refer to the Syntax information provided through : Get-Help GetProcess and compare the below examples
Get-Process can be used as below:

Get-Process name powershell computername localhost


Get-Process powershell computername localhost
Get-process computername localhost name powershell
Get-process powershell* -computername localhost
Get-process powershell*,s*

Aliases
A lot of cmdlets have pre defined aliases (Few examples we have already seen
include dir, ls for get-childitem and cd for set-location).
Get-Alias will list all the aliases available.
If you want to know the alias for a cmdlet use the parameter definition
Get-Alias definition Get-ChildItem

If you want to know the cmdlet for an alias then use the positional parameter
name as below
Get-Alias name dir
Get-Alias dir

We can also create or modify an Alias using:


Set-alias d get-childitem

Note that an Alias would only exist until the powershell console is closed. To
make it available beyond a session what can we do? <We will get there>

Conclusion
OBJECTIVE:
By now you should have an understanding on what scripting is. You should
have an idea about what powershell is, CMDLET structure, finding and
reading help and cmdlet information, and understand Alias concepts. From
here on you should be able to find a CMDLET if required and be able to use a
CMDLET when specified using the information available through Get-Help and
Get-Command.

References
Learn Windows Powershell 3 in a month of lunches, second edition by
Don Jones and Jeffery Hicks
Microsoft Windows Powershell Programming for the absolute
beginner, third edition by Jerry Lee Ford Jr.

Das könnte Ihnen auch gefallen