unable to run power shell script from SQL server agen

  • 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

  • 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.

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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.

  • 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

  • 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 }

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • 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