70 lines
2.5 KiB
PowerShell
70 lines
2.5 KiB
PowerShell
|
function Remove-AppSetting {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Removes an appSetting Key/Value pair in the specified file. Filepath defaults to the 64 bit machine config.
|
||
|
|
||
|
.DESCRIPTION
|
||
|
Remove an appSetting key/value pair in the specified config file if present.
|
||
|
Will default to the global 64 bit machine.config file if no config file value specified.
|
||
|
Will not tickle files where no values have changed.
|
||
|
Can connect to remote computers as well.
|
||
|
|
||
|
.PARAMETER Key
|
||
|
[string] The appSetting key
|
||
|
|
||
|
.PARAMETER FilePath
|
||
|
[string] The location to change settings in. Defaults to the global 64bit machine.config file.
|
||
|
|
||
|
.PARAMETER ComputerName
|
||
|
[string] The computer to connect to. Defaults to localhost
|
||
|
|
||
|
.PARAMETER Force
|
||
|
[switch] Allow forcing the write of the process. This is due to the option for ShouldProcess.
|
||
|
#>
|
||
|
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='None')]
|
||
|
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Internal function uses the SupportsShouldProcess flag, not us, this is good and proper")]
|
||
|
param (
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[string]$Key,
|
||
|
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[Alias("Path")]
|
||
|
[string]$FilePath = (Get-DotNetConfigPath -use64Bit $true),
|
||
|
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[string]$ComputerName = "localhost",
|
||
|
|
||
|
[Parameter(Mandatory = $false)]
|
||
|
[switch]$Force
|
||
|
)
|
||
|
|
||
|
$logLead = (Get-LogLeadName)
|
||
|
|
||
|
#region guard clauses
|
||
|
# If a computername was provided, modify the filepath to be a UNC path.
|
||
|
if((![string]::IsNullOrWhiteSpace($ComputerName)) -and ($ComputerName -ne "localhost")) {
|
||
|
$FilePath = (Get-UncPath -filePath $FilePath -ComputerName $ComputerName)
|
||
|
}
|
||
|
|
||
|
if (!(Test-Path -PathType Leaf -Path $FilePath)) {
|
||
|
Write-Warning "$logLead : Could not find a file at [$FilePath]. Execution cannot continue."
|
||
|
return ## exit early
|
||
|
}
|
||
|
|
||
|
Write-Verbose "$logLead : Reading Config file at [$FilePath]"
|
||
|
$xml = Read-XMLFile $FilePath
|
||
|
if(!$xml) {
|
||
|
throw "$logLead : Config at [$FilePath] could not be converted to xml."
|
||
|
}
|
||
|
|
||
|
Write-Verbose "$logLead : Ensuring configuration root node exists..."
|
||
|
if(!$xml.configuration){
|
||
|
throw "$logLead : How does [$FilePath] not have a root <configuration> element??"
|
||
|
}
|
||
|
#endregion guard clauses
|
||
|
|
||
|
Invoke-CommandWithRetry -MaxRetries 3 -SecondsDelay 1 -Arguments @($Key, $FilePath, $Force) -ScriptBlock {
|
||
|
param($Key, $FilePath, $Force)
|
||
|
Remove-AppSettingPrivate -Key $Key -FilePath $FilePath -Force:$Force
|
||
|
}
|
||
|
}
|