From 19bafc5ea51135c05b63610ca7ef5abb3cdc36e0 Mon Sep 17 00:00:00 2001 From: montereyharris Date: Thu, 18 May 2017 10:34:29 -0500 Subject: [PATCH 1/6] Updated Install-OMSAgent to properly use local path --- functions/public/Install-omsAgent.ps1 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/functions/public/Install-omsAgent.ps1 b/functions/public/Install-omsAgent.ps1 index cb7a1d0..cd75dae 100644 --- a/functions/public/Install-omsAgent.ps1 +++ b/functions/public/Install-omsAgent.ps1 @@ -26,23 +26,29 @@ function Install-OmsAgent [Alias('IPAddress', 'Name')] [string[]] $computerName = $env:COMPUTERNAME, - [Parameter(Mandatory=$true, ParameterSetName='workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceClearText')] [ValidateNotNullOrEmpty()] [string] $workspaceid, - [Parameter(Mandatory=$true, ParameterSetName='workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceClearText')] [ValidateNotNullOrEmpty()] [string] $workspacekey, - [Parameter(Mandatory=$true, ParameterSetName='workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceEncrypt')] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $workSpace, [Parameter(ParameterSetName='downloadOMS')] + [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceEncrypt')] [ValidateNotNullOrEmpty()] [string] $downloadURL = 'http://download.microsoft.com/download/0/C/0/0C072D6E-F418-4AD4-BCB2-A362624F400A/MMASetup-AMD64.exe', [Parameter(ParameterSetName='localOMS')] + [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceEncrypt')] [ValidateScript({Test-Path $_ })] [string] $sourcePath, @@ -79,7 +85,7 @@ function Install-OmsAgent $psSession = New-PSSession -ComputerName $computer @commonSessionParams Write-Verbose "[$(Get-Date -Format G)] - $computer - Checking if OMS is Installed" - + if(-not (Get-omsAgentInternal -computerName $computer -session $psSession)) { If ($Pscmdlet.ShouldProcess($computer, 'Install OMS Agent')) @@ -96,7 +102,7 @@ function Install-OmsAgent $path } - if($PSBoundParameters.sourcePath -eq $true) + if($PSBoundParameters.sourcePath) # Check for source path { Write-Verbose "[$(Get-Date -Format G)] - $computer - Copying files over powershell session" Copy-Item -Path $sourcePath -Destination (Split-path $path) -ToSession $psSession -Force From c1d5589f5cc55fb91a33eb6fe893834dfa802096 Mon Sep 17 00:00:00 2001 From: montereyharris Date: Fri, 2 Jun 2017 14:59:42 -0500 Subject: [PATCH 2/6] Fix Bug where it fails if sourcepath was different than default file name --- functions/public/Install-omsAgent.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/public/Install-omsAgent.ps1 b/functions/public/Install-omsAgent.ps1 index cd75dae..fc45e53 100644 --- a/functions/public/Install-omsAgent.ps1 +++ b/functions/public/Install-omsAgent.ps1 @@ -91,7 +91,7 @@ function Install-OmsAgent If ($Pscmdlet.ShouldProcess($computer, 'Install OMS Agent')) { $path = Invoke-Command -Session $pssession -ScriptBlock { - $path = Join-Path $ENV:temp "MMASetup-AMD64.exe" + $path = Join-Path $ENV:temp "MMASetup.exe" # Check if file exists and if so remove if(Test-Path $path) @@ -105,7 +105,7 @@ function Install-OmsAgent if($PSBoundParameters.sourcePath) # Check for source path { Write-Verbose "[$(Get-Date -Format G)] - $computer - Copying files over powershell session" - Copy-Item -Path $sourcePath -Destination (Split-path $path) -ToSession $psSession -Force + Copy-Item -Path $sourcePath -Destination $path -ToSession $psSession -Force } else { From fab928a4ebd63bddcb013f5bbe9016900937df74 Mon Sep 17 00:00:00 2001 From: montereyharris Date: Wed, 7 Jun 2017 12:04:07 -0500 Subject: [PATCH 3/6] Update Parameter Set names to camelCase --- functions/public/Install-omsAgent.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/functions/public/Install-omsAgent.ps1 b/functions/public/Install-omsAgent.ps1 index fc45e53..cf3151b 100644 --- a/functions/public/Install-omsAgent.ps1 +++ b/functions/public/Install-omsAgent.ps1 @@ -26,29 +26,29 @@ function Install-OmsAgent [Alias('IPAddress', 'Name')] [string[]] $computerName = $env:COMPUTERNAME, - [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] - [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='downloadOms-workSpaceClearText')] [ValidateNotNullOrEmpty()] [string] $workspaceid, - [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] - [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='downloadOms-workSpaceClearText')] [ValidateNotNullOrEmpty()] [string] $workspacekey, - [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceEncrypt')] - [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='downloadOms-workSpaceEncrypt')] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $workSpace, [Parameter(ParameterSetName='downloadOMS')] - [Parameter(Mandatory=$true, ParameterSetName='downloadoms-workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='downloadOms-workSpaceEncrypt')] [ValidateNotNullOrEmpty()] [string] $downloadURL = 'http://download.microsoft.com/download/0/C/0/0C072D6E-F418-4AD4-BCB2-A362624F400A/MMASetup-AMD64.exe', [Parameter(ParameterSetName='localOMS')] - [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceClearText')] - [Parameter(Mandatory=$true, ParameterSetName='localoms-workSpaceEncrypt')] + [Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceClearText')] + [Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceEncrypt')] [ValidateScript({Test-Path $_ })] [string] $sourcePath, From e37d1772256516ff430351468f61710a7c14225d Mon Sep 17 00:00:00 2001 From: montereyharris Date: Tue, 25 Jul 2017 17:28:47 -0500 Subject: [PATCH 4/6] Add Update-omsAgent to facilitate updating MMA or MOM agents manually --- omsAgent.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omsAgent.psd1 b/omsAgent.psd1 index 4431182..f2fe3bf 100644 --- a/omsAgent.psd1 +++ b/omsAgent.psd1 @@ -66,7 +66,7 @@ Description = 'A PowerShell module to aide with deployment and management of OMS # NestedModules = @() # Functions to export from this module -FunctionsToExport = 'Get-omsAgentWorkSpace', 'Get-omsAgent', 'Add-omsAgentWorkSpace', 'Remove-omsAgentWorkSpace', 'Update-omsAgentWorkSpaceKey', 'Install-omsAgent', 'Uninstall-omsAgent', 'Get-omsAgentProxy', 'Remove-omsAgentProxy', 'Add-omsAgentProxy', 'Install-OmsDependencyAgent', 'Get-omsDependencyAgent', 'Uninstall-omsDependencyAgent' +FunctionsToExport = 'Get-omsAgentWorkSpace', 'Get-omsAgent', 'Add-omsAgentWorkSpace', 'Remove-omsAgentWorkSpace', 'Update-omsAgentWorkSpaceKey', 'Install-omsAgent', 'Uninstall-omsAgent', 'Get-omsAgentProxy', 'Remove-omsAgentProxy', 'Add-omsAgentProxy', 'Install-OmsDependencyAgent', 'Get-omsDependencyAgent', 'Uninstall-omsDependencyAgent','Update-omsAgent' # Cmdlets to export from this module CmdletsToExport = '' From 6bcc1b5cae0b774407251a63b888d98beafca4d4 Mon Sep 17 00:00:00 2001 From: montereyharris Date: Tue, 25 Jul 2017 17:30:24 -0500 Subject: [PATCH 5/6] Added update-omsagent function --- functions/public/Update-omsAgent.ps1 | 179 +++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 functions/public/Update-omsAgent.ps1 diff --git a/functions/public/Update-omsAgent.ps1 b/functions/public/Update-omsAgent.ps1 new file mode 100644 index 0000000..406c6d9 --- /dev/null +++ b/functions/public/Update-omsAgent.ps1 @@ -0,0 +1,179 @@ +#Requires -Version 5.0 + +function Update-OmsAgent +{ + <# + .Synopsis + Update the OMS agent on remote computers. + .DESCRIPTION + Either downloads the installer from a URL or copies the installer via the powershell session. Can detected if a previous version is installed and skip if so. If allready installed WorkSpaceId and WorkSpaceKey added to previous install. Doesn't detect invalid workspace IDs or Keys. + .EXAMPLE + Update-OmsAgent -sourcePath 'c:\MMASetup-AMD64.exe' -Verbose + .EXAMPLE + Update-OmsAgent -computerName -Verbose + .NOTES + Written by ben taylor and Monty Harris + Version 1.0, 25.07.2017 + #> + [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'Low', DefaultParameterSetName='downloadOMS')] + [OutputType([String])] + Param ( + [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline=$True, valuefrompipelinebypropertyname=$true)] + [ValidateNotNullOrEmpty()] + [Alias('IPAddress', 'Name')] + [string[]] + $computerName = $env:COMPUTERNAME, + + [Parameter(Mandatory=$true, ParameterSetName='downloadOms')] + [ValidateNotNullOrEmpty()] + [string] + $downloadURL = 'http://download.microsoft.com/download/0/C/0/0C072D6E-F418-4AD4-BCB2-A362624F400A/MMASetup-AMD64.exe', + + [Parameter(ParameterSetName='localOMS')] + [ValidateScript({Test-Path $_ })] + [string] + $sourcePath, + + [Parameter(Mandatory = $false)] + [Parameter(ParameterSetName='downloadOMS')] + [Parameter(ParameterSetName='localOMS')] + [System.Management.Automation.PSCredential] + [System.Management.Automation.Credential()] + $Credential + ) + + Begin + { + $commonSessionParams = @{ + ErrorAction = 'Stop' + } + + If ($PSBoundParameters['Credential']) + { + $commonSessionParams.Credential = $Credential + } + } + Process + { + forEach ($computer in $computerName) + { + try + { + + Write-Verbose "[$(Get-Date -Format G)] - $computer - Creating Remote PS Session" + $psSession = New-PSSession -ComputerName $computer -EnableNetworkAccess @commonSessionParams + + Write-Verbose "[$(Get-Date -Format G)] - $computer - Checking if OMS is Installed" + + $orginalAgent = Get-omsAgentInternal -computerName $computer -session $psSession + if(($orginalAgent)) + { + If ($Pscmdlet.ShouldProcess($computer, 'Update OMS Agent')) + { + $path = Invoke-Command -Session $pssession -ScriptBlock { + $path = Join-Path $ENV:temp "MMASetup.exe" + + # Check if file exists and if so remove + if(Test-Path $path) + { + Remove-Item $path -force -Confirm:$false + } + + $path + } + + if($PSBoundParameters.sourcePath) # Check for source path + { + Write-Verbose "[$(Get-Date -Format G)] - $computer - Copying files over powershell session" + Copy-Item -Path $sourcePath -Destination $path -ToSession $psSession -Force + } + else + { + Write-Verbose "[$(Get-Date -Format G)] - $computer - Trying to download new installer from URL - $downloadURL" + Invoke-Command -Session $psSession -ScriptBlock { + Invoke-WebRequest $USING:downloadURL -OutFile $USING:path -ErrorAction Stop | Out-Null + } -ErrorAction Stop + } + + + Write-Verbose "$computer - Trying to Update OMS..." + $installString = $path + ' /Q:A /R:N /C:"setup.exe /qn AcceptEndUserLicenseAgreement=1"' + + $installSuccess = Invoke-Command -Session $psSession -ScriptBlock { + Get-service HealthService|Stop-Service + cmd.exe /C $USING:installString + $LASTEXITCODE + } -ErrorAction Stop + + Write-Verbose "[$(Get-Date -Format G)] - $computer - $installSuccess" + + if($installSuccess -eq 3010) + { + Write-Verbose "$computer - OMS updated correctly based on the exit code" + Write-Verbose "$computer - Restarting HealthService" + Invoke-Command -Session $psSession -ScriptBlock { + Get-service HealthService|Start-Service + } -ErrorAction Stop + + } + Elseif($installSuccess -ne 0) + { + Invoke-Command -Session $psSession -ScriptBlock { + Get-service HealthService|Start-Service + } -ErrorAction Stop + Write-Verbose "$computer - Restarting HealthService" + Write-Error "$computer - OMS didn't update correctly based on the exit code" + + + } + else + { + if((Get-omsAgentInternal -computerName $computer -session $psSession).displayverison -gt $orginalAgent.displayverison) + { + Write-Verbose "[$(Get-Date -Format G)] - $computer - OMS Updated correctly" + Invoke-Command -Session $psSession -ScriptBlock { + Get-service HealthService|Start-Service + } -ErrorAction Stop + } + else + { + Write-Error "[$(Get-Date -Format G)] - $computer - OMS didn't install correctly based on the exit code" + } + } + } + } + else + { + Write-Error "[$(Get-Date -Format G)] - $computer - OMS Agent not installed so skipping." + } + } + catch + { + Write-Error $_ + } + Finally + { + Write-Verbose "[$(Get-Date -Format G)] - $computer - Tidying up install files\sessions if needed" + + if($null -ne $psSession) + { + try + { + Invoke-Command -Session $pssession -ScriptBlock { + if(Test-Path $USING:path) + { + Remove-Item $USING:path -force -Confirm:$false + } + } -ErrorAction Stop + } + catch + { + Write-Verbose "[$(Get-Date -Format G)] - $computer - Nothing to tidy up" + } + + Remove-PSSession $psSession -whatif:$false -Confirm:$false + } + } + } + } +} \ No newline at end of file From dbe37142e77730a96a26c5d29857d27955f8475a Mon Sep 17 00:00:00 2001 From: montereyharris Date: Thu, 3 Aug 2017 15:53:40 -0500 Subject: [PATCH 6/6] Fix ParameterSet not resolving with Credential error --- functions/public/Install-omsAgent.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/functions/public/Install-omsAgent.ps1 b/functions/public/Install-omsAgent.ps1 index 9e1e282..c92eb34 100644 --- a/functions/public/Install-omsAgent.ps1 +++ b/functions/public/Install-omsAgent.ps1 @@ -52,7 +52,10 @@ function Install-OmsAgent [ValidateScript({Test-Path $_ })] [string] $sourcePath, - [Parameter()] + [Parameter(Mandatory=$false, ParameterSetName='localoms-workSpaceClearText')] + [Parameter(Mandatory=$false, ParameterSetName='downloadoms-workSpaceClearText')] + [Parameter(Mandatory=$false, ParameterSetName='localoms-workSpaceEncrypt')] + [Parameter(Mandatory=$false, ParameterSetName='downloadoms-workSpaceEncrypt')] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $Credential