Need help to run a Powershell from SQL Server

  • Hi All,

    I have a requirement to kick off a Powershell script and pass a .txt file with it.

    it works fine in Powershell ISE but I'm not able to run in from SQL Server.

    Get-Content D:\Temp\ServerList.txt | D:\Temp\StopDisableWindowsUpdateService.ps1

  • What is the error?

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Shawn Melton (11/9/2015)


    What is the error?

    ... and on which box is the "D:" drive?

    --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)

  • Possibly permissions as usually it is running under different credentials.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • I put the below command as a job step and made the step type as powershell.

    Get-Content D:\Temp\ServerList.txt | D:\Temp\StopDisableWindowsUpdateService.ps1

    Below is the Error I got.

    Executed as user: REDMOND\bgitsdsv. A job step received an error at line 3 in a PowerShell script. The corresponding line is '$space.ForegroundColor = $host.ui.rawui.ForegroundColor'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Exception setting "ForegroundColor": "Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"." '. Process Exit Code -1. The step failed.

  • try piping the output of the .ps1 to a file or something.

  • the powershell script is already writing the output to a .txt file. below is the script I'm trying to run.

    BEGIN

    {

    Clear

    }

    PROCESS

    {

    $server = $_

    set-service wuauserv -startuptype Disabled -computername $server -errorvariable errs 2>$null

    $d = get-date

    if ($errs.count -eq 0)

    {

    Write-output "$d :::::: SUCCEEDED :::::: $server :::::: The start type of service wuauserv has been set to Disabled" | out-file -append D:\Temp\StopDisableServiceLog.txt

    }

    else

    {

    Write-output "$d :::::: ERROR :::::: $server :::::: An error happened when trying to set the start type of service wuauserv to Disabled" | out-file -append D:\Temp\StopDisableServiceLog.txt

    }

    Invoke-command –computer $server –scriptblock {stop-service wuauserv} -errorvariable errs 2>$null

    if ($errs.count -eq 0)

    {

    Write-output "$d :::::: SUCCEEDED :::::: $server :::::: The service wuauserv has been Stopped" | out-file -append D:\Temp\StopDisableServiceLog.txt

    }

    else

    {

    Write-output "$d :::::: ERROR :::::: $server :::::: An error happened when trying to stop the service wuauserv" | out-file -append D:\Temp\StopDisableServiceLog.txt

    }

    }

  • Sounds like you are trying to access properties that the host is not implementing. Understandably the UI free PowerShell host does not support a foreground colour.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Why are you invoking a clear screen command in a script that should be pretty quiet and writing to a file?

  • In this case how can i proceed. My requirement is below.

    I'd like to stop and disable wuauserv service on servers which will be passed by a .txt file.

    set-service wuauserv -startuptype Disabled -computername $serverName

    Get-Service -Name SDOpsService -ComputerName $serverName | stop-service

  • You're trying to disable windows update from a script running in sql?

  • personally, if the answer to my previous is q is yes then i'd do something like:

    1) export my server list that i keep in a handy sql table to a text file or csv

    2) Pipe that text file into my .ps1

    3) Schedule the export for (1) in SQL Agent

    4) Schedule the .ps1 in Windows Scheduler on maybe your domain controller so it can hit all the servers/workstations on the network.

  • Yes. I'd like to disable Windows Update Service using Powershell in SQL. If this succeeds, i have whole bunch of SQL jobs to schedule which would get kicked off only at customer approved time for server patching.

  • Manic Star (11/11/2015)


    You're trying to disable windows update from a script running in sql?

    It has come to be a more common thing to do for some reason. I have seen other people have problems running this script from SQL Server.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Wouldn't it be easier to work with a network admin to do a group policy instead?

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic. Login to reply