103 lines
4.2 KiB
PowerShell
103 lines
4.2 KiB
PowerShell
function Set-DotNetCoreProfiling {
|
|
<#
|
|
.SYNOPSIS
|
|
Enables or disables profiling for a specific dotnet core microservice chocolatey package. The microservice still requires a restart before it will take effect.
|
|
.PARAMETER Name
|
|
The Name of the package to set the new-relic state for.
|
|
.PARAMETER Path
|
|
The Path of the package to set the new-relic state for.
|
|
.PARAMETER Enabled
|
|
True to enable New Relic, false to disable it.
|
|
#>
|
|
[CmdletBinding()]
|
|
Param(
|
|
[Parameter(ParameterSetName = 'ByPackageName', Mandatory = $true)]
|
|
[string]$Name,
|
|
|
|
[Parameter(ParameterSetName = 'ByPath', Mandatory = $true)]
|
|
[string]$Path,
|
|
|
|
[Parameter(ParameterSetName = 'ByPath', Mandatory = $true)]
|
|
[Parameter(ParameterSetName = 'ByPackageName', Mandatory = $true)]
|
|
[bool]$Enabled
|
|
)
|
|
|
|
$loglead = Get-LogLeadName
|
|
|
|
# $Name$Path will write both values, but it can only be one or the other. Cheap hack putting them both in the same brackets
|
|
Write-Host "$logLead : Setting CORECLR_ENABLE_PROFILING state for [$Name$Path]"
|
|
|
|
if ($PSCmdlet.ParameterSetName -eq "ByPackageName") {
|
|
# Construct the path to the microservice in the chocolatey install path.
|
|
$chocoPath = (Get-ChocolateyInstallPath)
|
|
$chocoPath = Join-Path $chocoPath "lib"
|
|
$chocoPath = Join-Path $chocoPath $Name
|
|
$Path = $chocoPath
|
|
} else {
|
|
# Name of service is choco lib folder Name
|
|
$Name = Split-Path -Path $Path -Leaf
|
|
}
|
|
|
|
$clrString = "CORECLR_ENABLE_PROFILING"
|
|
$regKeyPath = "HKLM:\SYSTEM\CurrentControlSet\Services\$Name"
|
|
$nullSeperator = $([char]0)
|
|
|
|
Write-Host "$loglead : Determining if [$Name] is Dot Net Core service"
|
|
try {
|
|
$packageManifest = Get-PackageManifest -Path $Path -PackageName $Name
|
|
$IsServiceManifestCore = Test-IsServiceManifestCore -ServiceManifest $packageManifest
|
|
}
|
|
catch {
|
|
Write-Warning "$logLead : Service [$Name] Unable to determine service runtime"
|
|
Write-warning $_
|
|
}
|
|
if ($IsServiceManifestCore -eq $true ) {
|
|
Write-Host "$logLead : Service [$Name] Setting [$clrString] [$($Enabled.toString())]"
|
|
if ($enabled -eq $true) {
|
|
$keyValue = "1"
|
|
} else {
|
|
$keyValue = "0"
|
|
}
|
|
try {
|
|
$shouldSetKey = $true
|
|
$rebuildString = ""
|
|
$regKey = Get-ItemProperty -Path $regKeyPath
|
|
$environmentKey = $regKey.Environment
|
|
|
|
[Array]$environmentValues = $environmentKey
|
|
foreach ($environmentValue in $environmentValues) {
|
|
if ($environmentValue -eq "$clrString=0") {
|
|
Write-Host "$loglead : [$environmentValue] CLR currently set to disabled"
|
|
if ($Enabled -eq $false) {
|
|
$shouldSetKey = $false
|
|
}
|
|
} elseif ($environmentValue -eq "$clrString=1") {
|
|
Write-Host "$loglead : [$environmentValue] CLR currently set to enabled"
|
|
if ($Enabled -eq $true) {
|
|
$shouldSetKey = $false
|
|
}
|
|
} else {
|
|
if ($environmentValue -ne $nullSeperator) {
|
|
Write-Host "$loglead : [$environmentValue] is not clr string"
|
|
$rebuildString = $rebuildString + $environmentValue + $nullSeperator
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Host "$loglead : Failed to find Core clr string"
|
|
}
|
|
if ($shouldSetKey -eq $true) {
|
|
if ((Test-Path -Path $regKeyPath) -eq $false) {
|
|
New-Item -Path $regKeyPath
|
|
}
|
|
$completeString = $rebuildString + $clrString + "=" + $keyValue
|
|
Write-Host "$loglead : Writing string $completeString to $regKeyPath"
|
|
Set-ItemProperty -Path $regKeyPath -Name "Environment" -Value ([string[]]($completeString)) -Force
|
|
} else {
|
|
Write-Host "$loglead : Registry key already set, skipping update to key."
|
|
}
|
|
} else {
|
|
Write-Host "$loglead : Runtime of $($packageManifest.ServiceManifest.runtime) for service [$Name] is not dot net core, skipping registry."
|
|
}
|
|
}
|