Thursday, April 26, 2012

Save PowerShell Console session commands and there output in to text file.

 

Hi,

These days i am very busy in some other projects so that's why this months i post only few articles. Sorry for that.

Does anytime you think to save all the commands and there output in to text file? it like storing all of the PowerShell Consoles session data.

If yes and you don't know how to do it , let me explain it.

it is very simple. we just need to use two commands these are: Start-Transcript , Stop-Transcript

Let see how to use it.

Open you Powershell console , and before running or doing anything else , type Start-Transcript and the path to txt file in which you want to save all the output.

Start-Transcript d:\Log.txt

You can notice in below screenshot it is showing that Transcript started and it the output file is D:\Log.txt.

26-04-2012 18-06-03

Now lets run Few Cmdlets.

Dir

26-04-2012 18-08-37

Get-Process

26-04-2012 18-09-31

one wrong command for testing error

26-04-2012 18-10-02

All done now, type Stop-Transcript, cmdlet to stop recording the session. Transcript is stopped and now lets open the Log.txt file and see if that store all the things which we have done in  our PowerShell console session.

26-04-2012 18-11-26

You can see in below screenshot  that everything which we have done is saved in to a text file. :)

 

26-04-2012 18-14-54

Thanks for viewing my blog.

Aman Dhally

Thursday, April 12, 2012

PowerShell Basics Part-2: Variables in PowerShell.

 

PowerShell Basics Part-1: Single Quotes (') and Double Quotes ("") in PowerShell.

 

images

Hi,

As i mentioned in the previous article that in the "PowerShell Basics" series we are going to cover the basics of PowerShell. Today we are covering variables. Variables plays an very important role in every scripting language and same in Powershell.

What are variables.?

Variables are just a container which can store anything {data} in it.  We can Store "Strings", Integers, Doubles, or Output of the commands.

How to create a variables.?

It very simple,  we can use New-Variable Cmdlet to create a new variable, Use -Name to named the variable and -Value to assign a value to it.

New-Variable -Name "Name" -Value "Aman"

12-04-2012 14-29-22

We just created a new variable , but

How we can see or access variables ?

use dollar sign $ with variable name to access it.

$name

12-04-2012 14-36-19

There is another and easy way for creating the variables. 

Just provide the name which you want to use preceding with $ sign and then type = equal to {assignment operator} and after that value.

$name = "aman"

12-04-2012 17-39-44

now try to access it using $name

12-04-2012 17-40-30

Variables and Strings

Adding String in Variables

To add strings in variable make sure that the desired string is  wrapped in single or double quotes.

12-04-2012 17-43-10

If you don't wrap in quotes you will get the below error.

12-04-2012 17-43-58

Integer and Variables

Saving integer in a variable is simple. Just assign the value to variable .

$var = 987

12-04-2012 17-46-52

Storing Output of Cmdlets

we can store the output of Cmdlets in to  the variables. You just need to specify the command which's output you want to store in a variable. I in below example i am saving  the output of Get-Process  in to variable $process.

$process = Get-Process

12-04-2012 17-50-29

and when you see the variable $process and you can see that it contains the Output of Get-Process.

12-04-2012 18-09-48

Assign a new value to variable.

use = equal to {equal assignment operator} to assign a new value. When you assigns a new value to variable the new value overwrites the old one.

In below example you can see that the old value of $name was "aman" but when we assigned a new value to $name we can see it overwrites the old one.

12-04-2012 18-17-38

Adding Multiple lines  in  single variable

we can use += {Plus Equal Assignment operator} to add or assign multiple lines to the variables. This is quite handy when you write

$body = "This is a Email "

$body += " Generate from Server"

$body += " Which is located in India"

 12-04-2012 18-27-25

Remove variables

To remove variable we can use remove-Variable cmdlet and after that provide variable name  .

remove-Variable body

12-04-2012 18-30-27

let check if the variable exists. Nope nothing there.

12-04-2012 18-32-12

 

Note: The value in variable are static and remain constant until you not changed them. For example you stored the value of Get-Process in a variable and after that you open notepad or any other app, the entry of new launched app wont be in the variable.

Thanks

Aman Dhally

Wednesday, April 11, 2012

PowerShell Basics Part-1: Single Quotes (') and Double Quotes ("") in PowerShell.

2012-01_SingleVsDoubleQuotes 

Hi,

When new users start working on PowerShell the the main problem which they have faced is basic of PowerShell or we can say basic of any scripting language.

In this "PowerShell basics" tutorial series we are planning to cover as much as basics of PowerShell we can. These basics are not in any particular manner. The things which come in my mind and start covering them.

In the Part-1 we are covering "Single Quotes (') and Double Quotes ("").

In PowerShell we can use Single quotes and double quotes to  access the folder and files which have space in there names. Without "single or Double quotes" PowerShell consider blank space as separator.

Have a Look.

Try to Get-Childitem of "Program files" with out space, and you will get an error that it not able to find "C:\Program". As i mentioned before that without quotes PowerShell treat "blank space " as separator.

11-04-2012 14-44-25

Now lets wrap the the "Program Files" in Double or Single Quote. And this time it works Perfectly.

11-04-2012 14-44-50

we can also use the single quotes to achieve the same. Let see another example.

I have a text file named as " Space test.txt", lets try to open it without quotes first.

11-04-2012 14-57-24

OOps !! same error..

11-04-2012 14-58-49

Wrap it Single or Double Quotes,  this time lets wrap it in a Single quote and see what will happen.  Working :)

11-04-2012 15-00-28

But..the main difference in Single and Double quotes are described below.

"Double Quotes"

The Main Difference between "single and Double quotes" are that Variable are expands in "Double Quotes" but they don't expand in single quotes.

$name = "Aman"

11-04-2012 15-36-47

In variable $name i am assigning a value "Aman" let see if variable expand in $name.

You can see that in  Write-Host "My Name is $name" , $name is changed with the value "Aman"

11-04-2012 15-38-04

'Single Quotes'

The Single Quotes mainly used to mention the path to files and folders. I already mentioned the main difference above between "single and double" quotes that variables don't get expand in Single Quotes.

Let's look at same above example but with single quotes.

$name = "Aman"

You can see that in Write-Host "My Name is $name" , $name is not changed, that remain constant.

11-04-2012 15-46-16

Thanks for Reading.

Aman Dhally

Wednesday, April 4, 2012

IP Address Release, Renew using PowerShell.


Hi,

This is a very common task which is nearly performed by every End-user and IT Admin and Engineers. Normally we use "IPCONFIG /Release" to release the IP-address and "IPconfig / Renew" to renew or getting a new IP Address.

In some powershell script you may need to do this ip config /release and renew task. We can use native "IPconfig" command to achieve this. But there is an another way to do the same. Using WMI.
Download the script from here :

In variable $ethernet we are querying class Win32_NetworkAdapterConfiguration and using where-Object cmdlet we are choosing those adapter which have IP Enabled and have DHCP Enabled selected in there IP Configuration

than on each adapter we are using wmi method ReleaseDHCPLease()  first and then RenewDHCPLease() .

Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

################################
$ethernet = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true}  
foreach ($lan in $ethernet) {
       Write-Host "Flushing IP addresses" -ForegroundColor Yellow
       Sleep 2
       $lan.ReleaseDHCPLease() | out-Null
       Write-Host "Renewing IP Addresses" -ForegroundColor Green
       $lan.RenewDHCPLease() | out-Null
       Write-Host "The New Ip Address is "$lan.IPAddress" with Subnet "$lan.IPSubnet"" -ForegroundColor Yellow
       }
#############################################
Download the script from here: http://gallery.technet.microsoft.com/scriptcenter/Renew-IP-Adresses-Using-365f6bfa

30-03-2012 15-41-48

I hope that it may help someone.

Thanks

Aman Dhally