Thursday, June 7, 2012

Export all jobs from a SQL server

To export all jobs from a SQL server.

Open ssms,connect to your sql server.Navigate to SQL Server Agent-- jobs.

Click View and select Object Explorer Details or press F7. The Object explorer details window should open on the right. Select all the jobs,right click  select Script job as-Create to -- File. Select a location and give any file name and save.

All the jobs would be exported as a single file and you can run that on any other database to restore all the jobs.

 

 

Saturday, April 21, 2012

Comparing two folders.

I recently was backing up my data and the backup device had lots of space so i turned up copying folders from multiple directories to the disc. But now was at loss when it came to deleting data from my hard drive. So wrote this little script. Thanks to Ying lin for showing the contains operator in powershell.

http://www.myitforum.com/articles/1/view.asp?id=10092

#usage = .\Compare-Items-in-two-directories.ps1 -path1 "e:\to write\dataondisk" -path2 "f:\dataoncd"

param([String] $path1,$path2)

$Diskdir = @()

$CDdir = @()

Get-childitem -Path$path1-recurse | %{$Diskdir=$Diskdir+$_.name}

$Diskdir.count Get-childitem -Path$path2-recurse | %{$CDdir=$CDdir+$_.name} $CDdir.count

$arraymatch= @()

$arraynotmatch= @()

foreach ($Itemin$Diskdir)

{

if($CDDir-contains$Item)

{

$arraymatch=$array+$item

}

else      {

$arraynotmatch=$arraynotmatch+$item

}

}

$arraynotmatch

 

Thursday, March 1, 2012

How to open Event log of remote computer using powershell

$Log = Get-Eventlog -name application -computername remotecomputer

If the following error is thrown.

Get-EventLog : The network path was not found.

Check if the remote registry service is started on the remote computer

Monday, February 13, 2012

Creating a library of scripts

I found this nice thread for invoking powershell scripts from another.

http://blogs.msdn.com/b/powershell/archive/2007/06/19/get-scriptdirectory.aspx

Since there is a script I keep calling for pulling the current data.

This came in useful. What I am doing is passing my other scripts a list of servers.

This list of servers keeps changing constantly and is updated into a SQL table. So every time I wanted a current list of servers I had to connect to SQL and pull the list manually

and present that to the script I wanted to run on that list.

So I found a good script which would allow me to query the table within power shell and pass that list to any other script.

I just add the following lines to the script I want to run on all the servers.

$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path
 
. "$ScriptDir\Lib.ps1"

Where lib.ps1 is my script querying the database.

 

Access variables or data of one script from another.

First script stored at C:\Parag\Scripts\Powershell :

$a = @("asdf","123")
return $a

Second script:

$scriptdir = "C:\Parag\Scripts\Powershell"
$c = . $scriptdir\first.ps1
$c