Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Agent job with powershell Expand / Collapse
Author
Message
Posted Thursday, June 19, 2014 5:32 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 8, 2014 11:44 AM
Points: 133, Visits: 282
Hi
I am trying to create an agent job that looks for a file to exist and file if the file is not fount. I am using a try\catch block but the job will not fail. Any ideas?

try {
Get-Item -Path E:\Migration Files\myfile.txt -ErrorAction Stop
} catch {
# File Does Not Exist
}
Post #1584098
Posted Thursday, June 19, 2014 8:29 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:39 AM
Points: 12,906, Visits: 31,985
i've used Test-Path to see if files or folders exist, and make a decision from there to do my logic; create files, process files, copy them or whatever.

#the detail file of this database:
$filename = $dir + $NiceServerName + '\'
if (!(Test-Path -path $filename))
{
New-Item $filename -type directory
}



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1584136
Posted Thursday, June 19, 2014 8:35 PM This worked for the OP Answer marked as solution


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 8, 2014 11:44 AM
Points: 133, Visits: 282
Thanks
I didn't want to add any other logic to the script just have the job step fail if the file is present. Someone suggested just using the Get-item by itself and it did the trick.
Get-Item -Path E:\Migration Files\myfile.txt -ErrorAction Stop
Post #1584140
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse