September 10, 2013 at 1:39 pm
hello,
i created this power shell script to retun SQL server configurationsconfiguration, the script works fine when I run it from Powershell ISE, however it fails when i schedule it to run as power shell step in sql agent as ajob, it gives me syntax error, any ideas what I'm doing wrong?, i read some articles about assigning the get-date to a variable, i tried it without any lock
Import-Module SQLPS -DisableNameChecking
#One way to map
New-PSDrive -Name UNCPath -PSProvider FileSystem -Root \\Share\Servers\
foreach ($instancename in get-content -path UNCPath:\Servers.txt)
{
$Date =Get-date -Format "yyyy-m-d_hmmtt"
$folder= "UNCPath:\servers"
$filename = "$($instanceName)_Configuration_$($Date).csv"
$fullpath =Join-Path $folder $filename
$server = New-Object 'Microsoft.SqlServer.Management.Smo.Server' -ArgumentList $instanceName
$server.Configuration.Properties | Export-Csv -Path $fullpath
}
remove-psdrive -name UNCPath
September 10, 2013 at 2:22 pm
try changing
$filename = "$($instanceName)_Configuration_$($Date).csv"
to
$filename = "$instanceName_Configuration_$Date.csv"
I wasn't able to get it to run in my test but making after making this change i no longer got the bad syntax error.
September 10, 2013 at 2:23 pm
mhafiez (9/10/2013)
hello,i created this power shell script to retun SQL server configurationsconfiguration, the script works fine when I run it from Powershell ISE, however it fails when i schedule it to run as power shell step in sql agent as ajob, it gives me syntax error, any ideas what I'm doing wrong?, i read some articles about assigning the get-date to a variable, i tried it without any lock
Import-Module SQLPS -DisableNameChecking
#One way to map
New-PSDrive -Name UNCPath -PSProvider FileSystem -Root \\Share\Serversforeach ($instancename in get-content -path UNCPath:\Servers.txt)
{
$Date =Get-date -Format "yyyy-m-d_hmmtt"
$folder= "UNCPath:\servers"
$filename = "$($instanceName)_Configuration_$($Date).csv"
$fullpath =Join-Path $folder $filename
$server = New-Object 'Microsoft.SqlServer.Management.Smo.Server' -ArgumentList $instanceName
$server.Configuration.Properties | Export-Csv -Path $fullpath
}
remove-psdrive -name UNCPath
I strongly recommend that you post the text of the exact error.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 10, 2013 at 2:28 pm
the error message i got from sql agent history is:
step Name1
Duration00:00:00
Sql Severity0
Sql Message ID0
Operator Emailed
Operator Net sent
Operator Paged
Retries Attempted0
Message
Unable to start execution of step 1 (reason: line(14): Syntax error). The step failed.
September 10, 2013 at 2:39 pm
thanks Bob, i changed it to $filename = "$instanceName_Configuration_$Date.csv"
and started the sql job with the same error i had before syntax error, i can run the script inside ISE without any issues
September 10, 2013 at 3:31 pm
i wasn't paying attention, I had the filename string incorrect.
try this:
$filename = $instanceName+"_Configuration_"+$Date+".csv"
I was able to get it to run successfully with a modified script on sql server 2008 r2. looks like you cant use import-module.
foreach ($instancename in get-content -path e:\temp\server.txt)
{$Date =Get-date -Format "yyyy-m-d_hmmtt"
$folder= "e:\temp\"
$filename = $instanceName+"_Configuration_"+$Date+".csv"
$fullpath =Join-Path $folder $filename
$server = New-Object 'Microsoft.SqlServer.Management.Smo.Server' -ArgumentList $instanceName
$server.Configuration.Properties | Export-Csv -Path $fullpath }
September 10, 2013 at 4:54 pm
Bob,
Thanks alot, it worked, appreciate it.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply