Powershell

advertisement
RYAN SANDERS
POWERSHELL
POWERSHELL
By: Ryan Sanders
Powershell | Ryan Sanders
Windows has use command line since the first design of Windows systems such as DOS
and command prompt. While this was a good tool for minor task with limited options, Microsoft
decided they need a more powerful tool that system administrators could make use of in order to
increase productivity and make their jobs much easier. Microsoft attempted to make things easier
by introducing Windows Script Host in 1998.1 This attempt was unsuccessful as it was very hard
to learn and there was little to no documentation on it. So by 2002 the developers decided to
create an all new program that would allow system administrators to automate their various
tasks. This would be an extension to the command shell Windows was already using and was
originally called Monad (also known as Microsoft Shell or MSH).2 Beta testing started in 2005
and beta testing continued on till its release in 2006. With its release Microsoft changed the name
from Monad to Windows Powershell so that it would look like a more integrated part of
Windows. Powershell was capable of not only running the standard command line code that
many systems administrators used but also allowing scripts to be written within its GUI allowing
for automating mundane tasks and the ability to save scripts for later use so that systems
administrators were not tied up with repetitive work. Powershell has under gone various changes
with its most current version being version 4, though most individuals are still using versions 2
or 3.
While Windows Powershell is considered a programing language, it’s really more of a
systems administrative scripting language. Variable names are not case sensitive and can be a
letter or string, numeric, contain spaces or special characters (though it is ill advised as it can
become very difficult and confusing), or any combination but must start with the $ sign.3
1
(Wikipedia) Background Paragraph 2
(Wikipedia) Background Paragraph 3
3
(Microsoft) hh847734.aspx
2
1
Powershell | Ryan Sanders
Windows Powershell variables are unique in that they can contain “a collection services and a
collection of processes.”4 Powershell is also very unique from other programing languages as
well as scripting languages as everything within Powershell “is treated as an object.” 5 So
Powershell is the most object-oriented language out today. Since everything is an object, there
are not only the standard data types we see in every other language out there but you are able to
create and execute your own data type. While most languages require you to specify the data
type for the variable, Windows Powershell does this for you. This could be annoying if you like
to declare your variable data type however you can still make numbers into strings by inclosing
them in quotes. Powershell sees whatever the variable equals and determines the data type by
whether it is a number (thus it becomes an integer, double, or float), a string enclosed with
quotation marks as a string, or a command if no quotation marks are used at the beginning of the
variables value. Variables can only be accessed within the scope that it was declared as well as
all child scopes unless the variable was declared as a global or private variable. We have seen
this done with most programing languages created throughout the years. While exception
handling is done with the try, catch, final block error handling requires you to create a variable in
order to output error. If your errors however are not specific enough you could also declare your
variable to equal $_.exception.message which outputs the specific error that has occurred.
Now I have mentioned that Powershell determines the data type for you but I feel it is
important to mention that while most programs require the data types to be the same before it
will ether calculate (such as integer vs double) them or combine strings together, Powershell
does not. You can have an integer variable calculated with a different numerical data type value
as well as combining numerical data with strings. Arrays, hash tables, and any other data type
4
5
(Jones) p. 169
(Jones) p. 170
2
Powershell | Ryan Sanders
you could possible think of are incorporated within Powershell as well as for and while loops
allowing you to run scripts or sections of scripts as many times as needed. This is very handy if
you have multiple entries and would like to declare as few variables as needed. Say you needed
to enter several names into a database. You could declare a variable for the number of names to
be added and a variable for the name. Then all you would have to do is create your script to run x
number of times and after every name entered into the database you would simply write over the
data stored within the name variable or have the script read a text file that would input the
individual names writing over the data until the script is stopped.
With variables being able to store command line arguments, scripts can call upon other
scripts to run inside them by simply using a command to call the script to run. The ability to run
scripts within scripts must be used with caution as Powershell uses a top down approach to read
and execute commands. While functions, if statements, and while statements use curly brackets
to signify the statements within them are in essence one command. Other commands are ran as
Powershell sees them and by simply writing a new command on a separate line tells Powershell
that the first command is done and this is a new command. No punctuation need to symbolize the
end of the line. We should also take this time to note that Powershell does in fact allow for
concurrency however do to the complexity and the nature of what Powershell is used for, this is
not recommended as you could really destroy your system or remote system if you are not
careful. Yes I did mention a remote system because Powershell offers you the capabilities to
remote into other computers or even servers allowing the system administrator to run commands
and scripts remotely. Keep in mind that the computer or server you wish to remote into must be
setup beforehand to except remote connection from Powershell and that all remote connect,
3
Powershell | Ryan Sanders
while not necessary, should be setup to connect through https in order to keep from sending plain
text passwords across the internet.
While Powershell allows these remote connection we could also write a script to connect
for us. Not just one server or computer but several. You could even utilize a command to connect
to a localhost which creates a temporary copy of your computer so that you can test or practice
remote computing. Just keep in mind that everything you open for remote connections must be
secured against hacking attempts. Windows Powershell allows us to do this by only allowing
connections from trusted hosts as well as enabling “listeners by using Group Policy”. 6 Along the
lines of security you should also be cautious of who you give access to. With the ability to use
Powershell you are able to retrieve plan text usernames and passwords for system administrator
accounts or any account setup on the computer even with low level privileges. While this seems
like a vulnerability that Microsoft should have fix a long time ago, it does come in handy for
system administrators to recover user passwords or allow scripts access to passwords for
automatic connections to remote systems.
As with any programing or scripting language, Windows Powershell’s readability and
write ability are only as difficult as the programmer makes them. As long as the programmer
makes all his variable names easily understandable as to their function then reading the script
becomes very easy and efficient. This is also aided by naming the script to specify what the
script is designed to do. Write ability for beginners is somewhat difficult to do as there are so
many possible options that do major systems changes. It is not really possible to remember every
Cmdlet though you can always remember that commands begin with a verb followed by a noun
(i.e. Get-Process).
6
(Jones) p.110
4
Powershell | Ryan Sanders
We’ve see how life as a system administrator can be made easier by using Windows
highly reliable Powershell and its scripting capabilities. As we know this has a direct effect on
cost and must be taken into consideration as we send our systems admin out to for training on
new programs. While Windows Powershell cost nothing due to the fact that it comes standard on
all computer that have the Windows OS installed as well as Windows Servers. So instead of
costing any company we can safely say that this tool is a money maker as it increases
productivity and efficiency of our system administrators as we all know time is money.
5
Powershell | Ryan Sanders
Appendices
----Script Example of Creating Personal Variables---# Create variables and assign to values
$amount = 120
$VAT = 0.19
# Calculate:
$result = $amount * $VAT
# Output result
$result
22.8
# Replace variables in text with values:
$text = "Net amount $amount matches gross amount $result"
$text
----End Script---Net amount 120 matches gross amount 142.87
---Sample of Data Types--$a = 1234; $a
$a.GetType().FullName
$a = 12345678910; $a
$a.GetType().FullName
$a = 1234.5678; $a
$a.GetType().FullName
$a = 1234.5678d; $a
$a.GetType().FullName
---End Sample---
8
7
8
(Weltner) Ch. 3 Personal Variables
(Sheldon) Paragraph 7
6
Powershell | Ryan Sanders
Works Cited
Jones, Don. "Learn Windows Powershell in a Month of Lunches." Jones, Don. Learn Windows Powershell
in a Month of Lunches. Shelter Island: Manning Publications Co., 2011. 309. Book.
Microsoft. Microsoft Tech Net. 2014. Web. <http://technet.microsoft.com/enus/library/hh847840.aspx>.
Sheldon, Robert. Windows IT Pro. 22 Febuary 2009. <http://windowsitpro.com/powershell/workingpowershells-data-types>.
Sheppard, Simon. ss64. n.d. <http://ss64.com/ps/>.
Weltner, Dr. Tobias. n.d. <http://powershell.com/cs/blogs/ebookv2/default.aspx>.
Wikipedia. 1 December 2014. Website. October 2014.
<http://en.wikipedia.org/wiki/Windows_PowerShell>.
7
Download