Using PowerShell on the Cloud
PowerShell is a scripting environment developed by Microsoft for Windows. It has been commercially available for various versions of Windows since 2006, either as an optional or integrated feature. PowerShell consists of a command shell and scripting language that runs within .NET Framework. It can use COM and WMI, allowing system administrators to automate tasks. PowerShell is especially useful for managing remote systems such as cloud servers.
Background
All versions of DOS and Windows have some type of command-line interface such as command.com or cmd.exe. These interfaces have basic commands which a user can execute from the command line or in a batch file. A major limitation of these early shells is that they are unable to automate all aspects of the Windows GUI, primarily because the scripting language does not permit complex scripts.
This situation began to change with Version 1.0 of PowerShell, which Microsoft released for Windows Server 2003, Windows Vista and Windows XP. Version 2.0 is integrated with Windows Server 2008 R2 and Windows 7. PowerShell version 3.0 is integrated with Windows server 2012 and Windows 8.
Cmdlets
PowerShell generally uses .NET classes called cmdlets to perform operations. It can also instantiate other .NET classes, COM objects and WMI objects. An administrator can combine cmdlets into groups and use them in commands such as scripts or executables. These commands can access data sources such as the file system and system registry, which PowerShell providers make available to the command at run time.
Cmdlets are native commands that the PowerShell process executes after it binds the arguments in the command to its corresponding parameters.
A cmdlet generally has the naming convention - to make the name more descriptive, such as Get-Process. The output of a cmdlet is an object or an array of objects, and it can also receive these data types as input. A cmdlet's instance can override the methods of its base class such as BeginProcessing(), EndProcessing() and ProcessRecord(). PowerShell invokes each method of these classes in order, and it also calls ProcessRecord() if the cmdlet receives input. All cmdlet classes contain CmdletAttribute, a .NET attribute that specifies the verb and noun in the cmdlet's name.
Scripting
PowerShell's scripting language has several features that allow it to implement complex operations. It has dynamic typing, meaning that PowerShell performs most of its type checking at run time rather than compile time. This scripting language also supports essential programming elements such as functions, variables, branching and looping. Advanced programming features of PowerShell scripts include structured error handling.
The name of a script variable in PowerShell begins with the "$" character and you can assign any value to a variable.
Use single and double quotes to enclose a string, but a variable will only expand when it is enclosed by double quotes. Enclose a variable name with braces to indicate the variable is a file path. The contents of the variable will be written to the file if the variable is used as an L-value, which references the location of the variable. The contents of the variable will be read from the file if the variable is used as an R-value, which references the contents of the variable.
PowerShell uses the same dot notation to access the member of an object as C#. For example, myObject.myMember refers to the member named myMember of the object named myObject. A PowerShell script also has access to special variables that provide additional functionality. $args is an array that contains all of the arguments that the command line passed to a function. The $_ variable refers to the object that is currently in the PowerShell pipeline.
Functions
Declare a function in PowerShell with an optional parameter list by using the following syntax:
Function function_name (parameter list) {script block}
The following example declares a function called myDate that executes a cmdlet called Get-Date:
Function myDate {Get-Date}
Call a function with multiple arguments by using the following syntax:
function_name argument1 argument2
Note that the argument list is delimited by spaces rather than commas, as is the case in many other languages. These arguments may be bound to the parameters that you declared in the function definition, and you may also access arguments in the $args array by referencing their position within that array.
Call a function using an array as an argument with the following syntax:
function_name (argument1, argument2)
This example uses a single array as an argument which contains argument1 and argument 2 as two of its elements.
Call a .NET method from PowerShell with the following syntax:
[object_name.method_name]::static_method(argument list)
Examples
The PowerShell prompt will appear as "PS C:\>" in these examples. The following example stops all processes that begin with the letter "a":
PS C:\> Get-Process a* | Stop-Process
The syntax in the above example will be familiar to users of other command-line interfaces. The call to Get-Process returns a list of the running processes that start with the letter a. The pipe character passes this list to Stop-Process, which stops these processes.
A system administrator for a cloud server will often need to wait until a particular process is no longer running before performing a task. The following example shows how to accomplish this:
PS C:\> $MyProcess = Get-Process svchost
PS C:\> $MyProcess.WaitForExit()
The first line in this example declares a script with the name $MyProcess that calls the Get-Process cmdlet for a process named "svchost". The second line invokes the WaitForExit method of MyProcess, which waits for svchost to end before executing the next line.
You can also accomplish the same thing with the following example:
PS C:\> (ps svchost).WaitForExit()
This line uses the standard alias "ps" to call the Get-Process cmdlet for the svchost process. It then invokes the WaitForExit method for that instance of Get-Process.
Summary
The data centers that contain cloud servers are currently trending towards a lights-out mode of operation, meaning these centers don't have on-site personnel. This operational model relies on redundant hardware and automated backups, which give these servers the appearance of 100 percent uptime to their users. Cloud computing therefore involves automating systems management on a large scale, which is much easier when using PowerShell.
Sources
http://msdn.microsoft.com/en-us/library/ms714658.aspx
http://blogs.msdn.com/b/PowerShell/archive/2008/05/25/PowerShell-and-wpf-wtf.aspx
Copyright © 2018 McMahan Writing - All Rights Reserved.
Powered by GoDaddy