Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Install-omsAgent for issue #2 #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Added update-omsagent function
montereyharris-mag committed Jul 25, 2017
commit 6bcc1b5cae0b774407251a63b888d98beafca4d4
179 changes: 179 additions & 0 deletions functions/public/Update-omsAgent.ps1
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the description be updated so it is easier to understand exactly what the CMDLet does.

.EXAMPLE
Update-OmsAgent -sourcePath 'c:\MMASetup-AMD64.exe' -Verbose
.EXAMPLE
Update-OmsAgent -computerName <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',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure about specifying a download link here as it may not be a current one. My feeling is to make the user specify there own DL link so they can make sure it is current, incase the default link is a old version. Thought?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree however it is very difficult to find the download link at least in experience. I will test the link and see what version is being downloaded or see if there is an aka link out there


[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..."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add [$(Get-Date -Format G)] to verbose output to keep consistant.

$installString = $path + ' /Q:A /R:N /C:"setup.exe /qn AcceptEndUserLicenseAgreement=1"'

$installSuccess = Invoke-Command -Session $psSession -ScriptBlock {
Get-service HealthService|Stop-Service
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces around the pipe for readability.

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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add [$(Get-Date -Format G)] to verbose output to keep consistant.

Write-Verbose "$computer - Restarting HealthService"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add [$(Get-Date -Format G)] to verbose output to keep consistant.

Invoke-Command -Session $psSession -ScriptBlock {
Get-service HealthService|Start-Service
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces around the pipe for readability.

} -ErrorAction Stop

}
Elseif($installSuccess -ne 0)
{
Invoke-Command -Session $psSession -ScriptBlock {
Get-service HealthService|Start-Service
} -ErrorAction Stop
Write-Verbose "$computer - Restarting HealthService"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add [$(Get-Date -Format G)] to verbose output to keep consistant.

Write-Error "$computer - OMS didn't update correctly based on the exit code"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add [$(Get-Date -Format G)] to verbose output to keep consistant.



}
else
{
if((Get-omsAgentInternal -computerName $computer -session $psSession).displayverison -gt $orginalAgent.displayverison)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add logic so if "(Get-omsAgentInternal -computerName $computer -session $psSession).displayverison -eq $orginalAgent.displayverison)" the service is started. Might make it a little more robust if something stopped the agent installing but didn't change the original install.

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