Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Friday, February 21, 2014

Simple Scripts and Functions in PowerShell

Turning Commands into Scripts

Now we are going to make any commands into scripts for automation.


See into above image. Get-WmiObject command is used to get Total size and Free size of computer.
Here I hardcoded ComputerName and Drive. So see into below image how I use variables to remove hardcode.



Now I am going to add parameters into my script.
We can use param() to make our script parameterize.


Now we are going to make parameterize function into our script. Following image shows the structure of function.
Here the line [CmdletBinding()] is used. This is that next click in formalization. This is where you really transform a function to full cmdlet.


You are done with scripting. Now save your script.
Test your script with PowerShell. Run PowerShell as Administrator. Go to directory where you save your scripts and simply just run script. And try to use function you make it.



Oops It gives error as you show in picture.
So here what happen when you run your script new line in stack is created and your function is stored into that stack. When script is finished the stack is cleared and your function is cleared.
So what’s next??
The solution for that is make PowerShell not to remove your stack data until you tell to remove or close the Powershell, even-if your script is finished.
To do that as shown in following picture use ‘.’ (dot) before you run the script.



Wednesday, February 19, 2014

Start Scripting with PowerShell

Variables

$ is used in PowerShell to create and use variables.
First time in Programming, you can use space in variable.
You can use latters, numbers, spaces and underscores in variables.
Example:
$MyVar = 7
${My Var} = “Test”
You can also give Variable type like string, integer.
Example:
                [String]$MyName = “Ronak”
                [int]$Test = 6
Now if you give $Test = “test” it will give error. Because $Test is fixed for type [int].
You can use variable as multiple types by using runtime type casting.
Example:
                $x = [int] 6  ß This will store 6 as integer in variable x
Now, $x = “Test” ß This will store string into variable x.
You can use all variable types which are available in .NET.
You can also use following commands:
  • New-Variable
  • Set-Variable
  • Get-Variable
  • Clear-Variable
  • Remove-Variable


Quotation Marks

Pay attention! Single quotes and Double quotes performs differently in PowerShell.
Double Quotes resolves all variables.
Single Quotes prevents substitutions.
Example:
$x = 2
"The Value of $x is $x" ß The Value of 2 is 2
'The Value of $x is $x' ß The Value of $x is $x
"The Value of `$x is $x" ß The Value of $x is 2

Back-trick (` sign available below Esc key) is used to prevent individual substitutions.

Getting started with PowerShell


The Purpose of PowerShell
PowerShell allow you to improve the windows management and helps to improve automation.
PowerShell manages real-time commands with large-scale integration.

How to install PowerShell
PowerShell V3 is comes with Windows Management Framework 3.0
PowerShell V3 is compatible with
  • Windows 8
  • Windows Server 2012
  • Windows 7 SP1
  • Windows Server 2008 R2 SP1
  • Windows Server 2008 SP2

Download Windows Management Framework 3.0
Prerequisite: Full .NET Framework 4.0
Windows XP and Windows Server 2003 can run PowerShell V2
TIP: Always run PowerShell as Administrator.

PowerShell Security Goals
By default PowerShell is secured.
It prevents mistakes by unintentional admins and users.
By clicking on scripts it will not execute scripts, so mistakes can be avoided.
.ps1 extension can be used with notepad or any text editor. You can also use PowerShell ISE. We will talk about this later.
We must have to type full path of file to access or execute file. Shorten paths are restricted to prevent mistakes.

Execution Policy
In PowerShell, there are some execution policies defined for security levels.
By default execution policy is Restricted, which does not execute the script.
Command to know your current execution policy:
        GET-ExecutionPolicy
Command to set execution policy:
        SET-ExecutionPolicy <TYPE>
TYPE can be like
  • Restricted
  • Unrestricted
  • AllSigned
  • Remotesigned
  • Bypass
  • Undefined

Windows PowerShell ISE
The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console.  For example, when you debug a script in the Windows PowerShell ISE, to set a line breakpoint in a script, right-click the line of code, and then click Toggle Breakpoint.

Feature of PowerShell ISE
  • Multiline editing: To insert a blank line under the current line in the Command pane, press SHIFT+ENTER.
  • Selective execution: To run part of a script, select the text you want to run, and then click the Run Script button. Or, press F5.
  • Context-sensitive help: Type Invoke-Item, and then press F1. The Help file opens to the Help topic for the Invoke-Item cmdlet.

To start Windows PowerShell ISE
  • Click Start, point to All Programs, point to Windows PowerShell V2, and then click Windows PowerShell ISE.
  • In the Windows PowerShell console Cmd.exe, or in the Run box, type, powershell_ise.exe



Freemarket.com Marketplace