Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions functions/public/Install-omsAgent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,36 @@ function Install-OmsAgent
[Alias('IPAddress', 'Name')]
[string[]]
$computerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$true, ParameterSetName='workSpaceClearText')]
[Parameter(Mandatory=$true, ParameterSetName='localOms-workSpaceClearText')]
Copy link
Owner

Choose a reason for hiding this comment

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

Remove '-' out of all Parameter Set names to keep in keeping with camel case

[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')]
Copy link
Owner

Choose a reason for hiding this comment

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

This parameter set name needs changing,

[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,
[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
Expand Down Expand Up @@ -79,13 +88,13 @@ function Install-OmsAgent
$psSession = New-PSSession -ComputerName $computer -EnableNetworkAccess @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'))
{
$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)
Expand All @@ -96,10 +105,10 @@ function Install-OmsAgent
$path
}

if($PSBoundParameters.sourcePath -eq $true)
if($PSBoundParameters.sourcePath) # Check for source path
Copy link
Owner

Choose a reason for hiding this comment

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

Remove comment as not needed

{
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
{
Expand Down
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
}
}
}
}
}
2 changes: 1 addition & 1 deletion omsAgent.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down