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

SQL Server Agent running powershell commands Expand / Collapse
Author
Message
Posted Tuesday, February 8, 2011 3:56 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 6:48 AM
Points: 22, Visits: 22
Hi All,

I am trying to run the following powershell script as a job from SQL server Agent (2008 R2), however, I
get:

"Unable to start execution of step 2 (reason: The job step contains tokens. For SQL Server 2005 Service Pack 1 or later, you must use the appropriate ESCAPE_xxx macro to update job steps containing tokens before the job can run.). The step failed."

I run other jobs as Type "powershell" defined steps with variables which works fine, but I cannot get my head around which bit is the "Token". All help appreciated.

Thanks,
Colin.

$source_dir = "B:\Database_Backups"
$target_dir = "\\machine-b\backup_Database_Backups"

foreach ($i in Get-ChildItem $source_dir)
{
if ($i.CreationTime -gt ($(Get-Date).AddDays(-2)))
{
Copy-Item $i.FullName $target_dir
}
}

Post #1060104
Posted Tuesday, February 8, 2011 4:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:57 PM
Points: 13,606, Visits: 10,490
I think the $ sign is misleading SQL Server Agent, as he thinks it is a job token, while in fact it is a powershell variable.

http://msdn.microsoft.com/en-us/library/ms175575.aspx




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1060119
Posted Friday, January 20, 2012 1:49 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 1:46 PM
Points: 321, Visits: 1,279
That was very helpful link...solved my problem and my job is running smoothly now .
thanks


Aim to inspire rather than to teach.
SQL Server DBA
Post #1239740
Posted Wednesday, February 1, 2012 5:23 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:52 PM
Points: 58, Visits: 239
What was the actual solution?
Post #1245404
Posted Thursday, February 2, 2012 12:43 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 6:48 AM
Points: 22, Visits: 22
Put the date to a variable first it is the Get-Date that seems to be the problem in my example above.

$source_dir = "B:\Database_Backups"
$target_dir = "\\machine-b\Database_Backups"
$d = Get-Date

Get-ChildItem $source_dir\*.* -include *.bak, *.trn | where {$_.CreationTime -gt ($d.AddDays(-1))} | Copy-Item -Destination $target_dir
Post #1245510
Posted Sunday, July 20, 2014 6:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 20, 2014 6:50 AM
Points: 1, Visits: 0
Thank you! It works for me to ($d = Get-Date).
Post #1594386
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse