Remote/Silent installation of SQL CU and/or SSMS

Welcome Forums General PowerShell Q&A Remote/Silent installation of SQL CU and/or SSMS

Viewing 1 reply thread
  • Author
    Posts
    • #191284
      Participant
      Topics: 1
      Replies: 0
      Points: -8
      Rank: Member

      IF possible, can I automate the transfer then installation of SQL Server (and separately SSMS) ?

      I have 2 code snippets, a) Install SSMS b) Attempt to silently install SQL Server CU, neither install

      a)========================================================================

      #Requires -RunAsAdministrator

      [CmdletBinding()]

      param (

      [parameter(Mandatory = $false)]

      [int]$WriteLog = 0

      )

      if(-not $PSScriptRoot) {

      $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent

      }

      $username = "ad\xxxxxxxx"

      $password = Get-Content 'c:\scripts\securestring.txt ' | ConvertTo-SecureString

      $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

      $Server = "xxxxxxxx"

      $remotePath = "\\$Server\c$\sqltemp\Patches\"

       

      $msg = ""

      $args = @()

      $args += "/install /quiet /norestart"

       

      if($WriteLog -eq 1) {

      $args += "/log SSMS_$(Get-Date -Format `"yyyyMMdd_HHmm`").txt"

      $msg = "InstallationLog: $PSScriptRoot\SSMS_$(Get-Date -Format `"yyyyMMdd_HHmm`").txt"

      }

      # Create SSL/TLS secure channel

      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

      # Start the download

      Write-Host "Download starting! Please wait.."

      $ProgressPreference = 'SilentlyContinue'

      $Domain = "https://msdn.microsoft.com/en-us/library/mt238290.aspx"

      $url = (Invoke-WebRequest -Uri $Domain -UseBasicParsing).Links

      $members = ($url | Get-Member).Name

      $filter = @()

      if ($members -match 'innerHTML') {

      $filter = 'innerHTML'

      }elseif ($members -match 'outerHTML') {

      $filter = 'outerHTML'

      }

      $href = ($url | Where-Object $filter -Match "Download SQL Server Management Studio").href | Select-Object -First 1

      $job = Start-BitsTransfer -Source $href -DisplayName SSMS -Destination "\\$Server\\C$\sqltemp\Patches\SSMS-Setup-ENU.exe" -Asynchronous

       

      while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) {

      Start-Sleep 5;

      } # Poll for status, sleep for 5 seconds, or perform an action.

       

      Switch($Job.JobState) {

      "Transferred" { Complete-BitsTransfer -BitsJob $Job; Write-Output "Download completed!" }

      "Error" { $Job | Format-List } # List the errors.

      default { Write-Output "You need to re-run the script, there is a problem with the proxy or Microsoft has changed the download link!"; Exit } # Perform corrective action.

      }

       

      # We close running SSMS processes

      if (Get-Process 'Ssms') {

      Stop-Process -Name Ssms

      }

       

      # Install silently

      Write-Output "Performing silent install..."

      Start-Process -FilePath "$remotePath\\SSMS-Setup-ENU.exe" -ArgumentList $args -Wait -Verb RunAs

      Invoke-Command -Credential $Cred -ScriptBlock { & Start-Process "\\$Server\\C$\Sqltemp\Patches\SSMS-Setup-ENU.exe" -wait -verb runas; write-host "Done"}

      Write-Output $msg

      Write-Output "All done!"

      b)========================================================================

      Clear-Host

      Set-ExecutionPolicy Unrestricted

      $RootHotfixPath = 'C:\Users\xxxxxxxx\Desktop\'

       

      $Hotfixes = @('ssms_setup_enu.exe')

       

      $Servers = Get-Content 'C:\Scripts\SQL_Servers.txt'

      $Servers = @(aServer)

       

      $switches = "/install /quiet /norestart "

       

      $username = "ad\xxxxxxxx"

      $password = Get-Content 'c:\scripts\securestring.txt ' | ConvertTo-SecureString

      $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

       

      Enable-PSRemoting -Force

       

      foreach ($Server in $Servers)

      {

      Write-Host "Processing $Server..."

      $needsReboot = $False

      $remotePath = "\\$Server\c$\sqltemp\Patches\"

       

      if( ! (Test-Connection $Server -Count 1 -Quiet))

      {

      Write-Warning "$Server is not accessible"

      continue

      }

      if(!(Test-Path $remotePath))

      {

      New-Item -ItemType Directory -Force -Path $remotePath | Out-Null

      }

       

      foreach ($Hotfix in $Hotfixes)

      {

      Write-Host "thotfix: $Hotfix"

      $HotfixPath = "$RootHotfixPath\" + $Hotfix

      Write-Host $Hotfixpath

      Write-Host $remotePath

      Copy-Item $Hotfixpath $remotePath

       

      Write-Host $Server

      Enter-PSSession -ComputerName $Server -Credential $cred

      Invoke-Command -ComputerName $Server -ScriptBlock { & $HotfixPath -ArgumentList " }

       

      Write-Host "& C:\Windows\PsExec -s \\$Server wusa C:\Temp\Patches\$Hotfix /quiet /norestart"

      Write-Host "Last Exitcode : " + $LastExitCode

      if ($LastExitCode -eq 3010) {

      $needsReboot = $true

      }

       

      }

       

      # Delete local copy of update packages

      # Remove-Item $remotePath -Force -Recurse

      if($needsReboot)

      {

      Write-Host "Restarting $Server..."

      Restart-Computer -ComputerName $Server -Force -Confirm

      }

      }

       

    • #191290
      Moderator
      Topics: 0
      Replies: 42
      Points: 388
      Team MemberHelping Hand
      Rank: Contributor

      Yes, you should be able to do that. Can I suggest wrapping your code in the [ pre ] and [ /pre ] tags, so it formats correctly?

Viewing 1 reply thread
  • You must be logged in to reply to this topic.