Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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 26, 2016 10:05 AM
Points: 157, Visits: 355
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:18 AM
Points: 14,460, Visits: 37,991
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
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 26, 2016 10:05 AM
Points: 157, Visits: 355
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