How to capture error in powershell?

Following is the method to capture error in powershell….

INTERVIEW PREPARATION IN 30 MINs – Read Following wires for a quick interview preparation.

  1. SQL Server DBA “Interview Questions And Answers”
  2. SQL Azure Interview Questions and Answers Part – 1
  3. SQL Azure Interview Questions and Answers Part – 2
  4. Powershell Interview Questions and Answers

If you think that any of ur command will throw error in powershell then write as follows after that command

-ErrorVariable MyError -ErrorAction Silentlycontinue
if($Myerror) {write-output $server “Not Found” | out-file -filepath $file -append}

Below is the complete script to find the RAM information from a set of servers

$servers = Get-Content “C:\Users\Username\Desktop\REP3 and REP2 work\Serverlist.txt”
$file = “C:\Users\Username\Desktop\REP3 and REP2 work\RamInfo.txt”
foreach($server in $servers)
{$Ram = gwmi -computer $server -class win32_computersystem -ErrorVariable MyError -ErrorAction Silentlycontinue
if($Myerror) {write-output $server “Not Found” | out-file -filepath $file -append}
else{
$Ram = $Ram[‘Totalphysicalmemory’]/(1024*1024*1024)
write-output $server “:” $Ram | out-file -filepath $file -append
}
}

Regards,
Online SQL/Powershell Classes @ http://tuitionaffordable.webstarts.com

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s