Powershell Day 6

Day 6 Functions and error handling

As everyone aware of what is function, so no need to give therotical infomration about function, I am directly going with simple examples

Simple function

PS C:\> Function first {“Powershell is good”}

PS C:\> first

Powershell is good

Passing parameter to function

PS C:\> function second ($a,$b){$a-$b}

PS C:\> second 43 23

20

No need to separate the parameter with commas,

Specify the data type for the parameter

PS C:\> function second ([int] $a,[int] $b){$a-$b}

PS C:\> second  43 32

11

Parameter type                                Description

Switches                              Switches are present or absent, such as Get-ChildItem –Recurse

Options                                                Options take an argument value, such as Get-ChildItem -Filter *.cs

Arguments                         These are positional and don’t really have a name associated with them
return statement

PS C:\> function myreturn {return ” Powershell”}

PS C:\> myreturn

Powershell

[math]::sqrt(27)

.the functions we create will be temporary as soon as we exit from Powershell it will be deleted, so if we want to keep the function permanent then script them in the flat file .ps1 and execute later whenever you want.

>>We can store the command in a variable and run that command from that variable (similar to Pointer C)

PS C:\> $i= gcm dir

PS C:\> &$i

Gcm – get-command

>> we can also assign command to variable.

PS C:\> $d=dir

PS C:\> $d |select name

Will work same as dir | select name

There is a command call psdrive which will show all the drives on your system includes “registry” drives.

Error Handling:

Whenever you run any errorrouse command you will get and error.

You can save /append that error into a flat file.

PS C:\> dir mynameisd 2>a.txt

PS C:\> type a.txt

Get-ChildItem : Cannot find path ‘C:\mynameisd’ because it does not exist.

At line:1 char:4

+ dir  <<<< mynameisd 2>a.txt

Or

Can save that error into variable

PS C:\> dir mynameisd -ErrorVariable errs1

Get-ChildItem : Cannot find path ‘C:\mynameisd’ because it does not exist.

At line:1 char:4

+ dir  <<<< mynameisd -ErrorVariable errs1

ErrorVariable allies is -ev

It has following properties

PS C:\> $errs1 |gm -type property |select name

Name

—-

CategoryInfo

ErrorDetails

Exception

FullyQualifiedErrorId

InvocationInfo

TargetObject

$Error[0]

If error occurred by default that error will stored in variable $error[0]

 

PS C:\> dir nosuchfile

Get-ChildItem : Cannot find path ‘C:\nosuchfile’ because it does not exist.

At line:1 char:4

+ dir  <<<< nosuchfile

PS C:\> $error[0]

Get-ChildItem : Cannot find path ‘C:\nosuchfile’ because it does not exist.

At line:1 char:4

+ dir  <<<< nosuchfile

PS C:\>

$? And  $LASTEXITCODE :

PS C:\> dir mynameisd

Get-ChildItem : Cannot find path ‘C:\mynameisd’ because it does not exist.

At line:1 char:4

+ dir  <<<< mynameisd

PS C:\> $?

False

———————

PS C:\> dir mynm

Get-ChildItem : Cannot find path ‘C:\mynm’ because it does not exist.

At line:1 char:4

+ dir  <<<< mynm

PS C:\> $LASTEXITCODE

1

———————

“When your previous command is having error $? Will be False and  $LASTEXITCODE will be non zero”

Trap:

In the script if you want to trap the error use trap

trap [<exceptionType >]

{

<statementList >

}

Following script will give you an error

&{

1

2/$zero

3

}

“Error”

And we can trap the error like this

&{

&{ Trap { “$_”;continue}

1

2/$zero

3

}

“Good”

}

Throw: -keyword

If we want to throw custom error we can use throw.

To debug the script

Set-PSDebug [-Trace <Int32>] [-Step] [-Strict ]

Set-PSDebug –Off

Advertisements
This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s