SQL Patching with powershell

  • I am trying to automate SQL Server patching using powershell. I am using a server as the central server where I hold the list of my database machines. I run a job from the central sever and copy over the exe for the patches to the C:\ drives of the respective machines on my list. Then, I would like to send in powershell commands to every server on my list to patch the SQL Server instances present there. However, the code above doesn't seem to work for a remote machine and I do not know why. Could someone help?

    [[

    $item = Invoke-Command -computername testbox-scriptblock {Get-ChildItem "C:\" | Where-Object {$_.Name -like "*sqlserver*" -and $_.Name -like "*kb*"}}$name = $item.Name$directory = $item.Directoryname$servicePackExec= $directory + $name"Starting Service Pack Installation..."$patchCmd = "$servicePackExec /Action=Patch /IAcceptSQLServerLicenseTerms /AllInstances"$script = [scriptblock]::Create($patchCmd)Invoke-command -ComputerName testbox-ScriptBlock $script

    ]]

  • the code is very badly formatted, im guessing it needs to be this, but you have an unassigned or echoed string

    $item = Invoke-Command -computername testbox -scriptblock {Get-ChildItem "C:\" | Where-Object {$_.Name -like "*sqlserver*" -and $_.Name -like "*kb*"}}
    $name = $item.Name
    $directory = $item.Directoryname
    $servicePackExec= $directory + $name
    "Starting Service Pack Installation..."
    $patchCmd = "$servicePackExec /Action=Patch /IAcceptSQLServerLicenseTerms /AllInstances"
    $script = [scriptblock]::Create($patchCmd)
    Invoke-command -ComputerName testbox -ScriptBlock $script

    The code you posted has no space between testbox and-scriptblock.
    Please post the formatted code you are running other than leaving people to second guess

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Hi Perry, 
    I couldn't figure out how to post formatted code alongside my question. Thanks for helping me reformat the code.
    What do you think I am missing in the code that I am unable to run the executable on remote servers while it compiles without any error?

  • Srl832 - Wednesday, March 1, 2017 11:14 AM

    Hi Perry, 
    I couldn't figure out how to post formatted code alongside my question. Thanks for helping me reformat the code.
    What do you think I am missing in the code that I am unable to run the executable on remote servers while it compiles without any error?

    How do you know you're picking up the patches in the correct order?
    Do you really only want to apply CUs what about SPs?
    Why do it in Powershell?

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

Viewing 4 posts - 1 through 3 (of 3 total)

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