patternsqlMinor
SQL Server Job Powershell is failing
Viewed 0 times
failingsqlpowershellserverjob
Problem
I have a SQL Server 2014 instance job that runs nightly that moves backups from the local drive to a network drive. I wrote it using PowerShell.
The job is failing with "reason: line(1): Syntax Error". I have it set to "Run as:" proxy_1.
proxy_1 has a credentialed name next to it that has access to the share. I don't have any principals selected.
What am I missing?
$mypath = $('F:\Backup\CRMDB01$CRMDB012014\Prod\Full\')
$mydestination = '\\stagingshare\fromprod\'
$myfile = gci $mypath | sort LastWriteTime | select -last 1
copy-item -path ($mypath + $myfile) -destination $mydestinationThe job is failing with "reason: line(1): Syntax Error". I have it set to "Run as:" proxy_1.
proxy_1 has a credentialed name next to it that has access to the share. I don't have any principals selected.
What am I missing?
Solution
The syntax error you are getting is from the
On the copy issue you will likely need to specify the property you want to pass into
Now
$() this is taken as a token in SQL Server Agent context...so it will always bark at that; removing it should fix the syntax error.On the copy issue you will likely need to specify the property you want to pass into
Copy-Item. Just passing $myfile is not going to work because that contains the full object of the output from gci. So change it to something like:$myfile = gci $mypath | sort LastWriteTime | select -last 1 -ExpandPropety FullName
Copy-Item -path $myfile -destination $mydestinationNow
$myFile will contain the full path to your last file:Code Snippets
$myfile = gci $mypath | sort LastWriteTime | select -last 1 -ExpandPropety FullName
Copy-Item -path $myfile -destination $mydestinationContext
StackExchange Database Administrators Q#125303, answer score: 2
Revisions (0)
No revisions yet.