Showing posts with label Get-process. Show all posts
Showing posts with label Get-process. Show all posts

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

Monday, January 9, 2012

Sending Text to Clipboard Everywhere using Powershell

In a previous tip you learned how to use clip.exe to send results to the clipboard. But what if you don't have clip.exe (let's say on Windows XP) or don't want dependencies?

Here's a clever alternative:

function Out-Clipboard {

 param(

  $text

 )

 Add-Type -AssemblyName System.Windows.Forms

 $tb = New-Object System.Windows.Forms.TextBox

 $tb.Multiline = $true

     

 if ($Input -ne $null) {

  $Input.Reset()

  $tb.Text = $Input | Out-String

 } else {

  $tb.Text = $text

 }

 $tb.SelectAll()

 $tb.Copy()

}

Use it like this:

PS> Get-Process | Out-Clipboard

It solely uses .NET Framework functionality that is available in all versions and modes of PowerShell

Use Out-GridView Requirements

 

PowerTip of the Day, from PowerShell.com

 

Out-GridView is a great way to present results in a “mini-Excel” sheet:

Get-Process | Out-GridView

However, Out-GridView has two requirements:.NET Framework 3.5.1 and the built-in script editor ISE must both be installed. ISE is not installed by default on Windows Servers. So, if you want  to use Out-GridView on server products, you will need to make sure you install the ISE feature.

On a Server 2008 R2, you could enable ISE by using PowerShell:

Import-Module ServerManager

Add-WindowsFeature PowerShell-ISE

Thanks

Tuesday, November 22, 2011

Outputting Text Reports without Truncating

 

PowerTip of the Day, from PowerShell.com:

If you want to capture PowerShell results in a text file, you can redirect the results or pipe them to Out-File. In any case, what you capture is the exact representation of what would have been displayed in your PowerShell console. So, depending on the amount of data, columns may be missing or truncated.
Here is a function called Out-Report. You can pipe any result to it to create a text file that does not truncate columns. In addition, with the –Open switch parameter, you can open the resulting text file, too:
   1: function Out-Report {
   2:  
   3:  param(
   4:  
   5:   $Path = "$env:temp\report.txt",
   6:  
   7:   [switch]$Open
   8:  
   9:  )
  10:  
  11:  
  12:  
  13:  $Input |
  14:  
  15:   Format-Table -AutoSize |
  16:  
  17:   Out-File $Path -Width 10000
  18:  
  19:  
  20:  
  21:   if($Open) { Invoke-Item $Path }
  22:  
  23: }

Try it:

Get-Process | Out-Report -Open

Thanks

Aman

Tuesday, November 8, 2011

Close multiple “Not responding” Processes or Applications using “PowerShell”

 

Hi,

Yesterday I was working on Powerpoint, Word, and Excel simultaneously  and the files which i have opened in these programs was very heavy in size. so after few hours of continuous working on that these application got “Hanged” and they were "stopped Responding” .

Then I open “Task Manager”, select the each process and Kill them manually.

at that point I think, there should be some “PowerShell Way” to achieve the same task using script.

Yes there, Let me share it with you.

The cmdlets which we use to accomplish are: Get-Process , Where-Object, Get-Member (alias is gm)

Let’s Start :

Open PowerShell Console

type Get-Process and (|) Pipe to another cmdlet Get-Member or you can use the alias GM ,

Capture 0

this command shows your the all properties  and Methods used by Processes.

We are Interested the only two member properties, one is the Responding Property which in boolean (either True or False) and another one is Kill() which in Method to kill the process

Capture-0

Capture-5

lets begin.

for example :

My Notepad is stopped working and lets close it using PowerShell

Capture

type the command

Get-Process   | Where-Object { $_.responding -eq $false}

get-process will get the list of all running process and then piped to another command, the Where-Object command select only those services whose Responding status is False or we can say Not-responding

It found Notepad is not responding

Capture-2

but i want to put the above command in to variable, so if there is more then one process which are not responding they all get close in a single command.

$notres = Get-Process   | Where-Object { $_.responding -eq $false}

Capture-3

I used $notres variable and it stores our Get-Process | Where-Object { $_.responding -eq $false} command.

Now execute the Kill() Method and you will see that all “Not Responding” processes are closes now :)

$notres.Kill()

   Capture-4   

 

That’s All

 

Thanks for viewing

 

Aman Dhally

www.amandhally.net