Olá a pessoal.
A dica de hoje é uma função simples para validar a existencia de uma chave de registro no caminho especificado.
Muitas vezes em um script precisamos saber se uma chave existe ou nao no registro para determinada situação, para isso a função abaixo irá retornar True ou False se a chave existir ou não. Com essa função podemos automatizar processos que dependem da existencia de uma chave de registro ou para validar se ela foi efetivamente criada:
function Test-RegistryKey {
<#
.SYNOPSIS
Tests if a particular registry key exists.
.DESCRIPTION
Tests if a particular registry key exists within a specific path within the hive.
.PARAMETER ParameterA
The path to the registry hive.
.PARAMETER ParameterB
The Registry Key inside the registry Hive.
.EXAMPLE
Test-RegistryKey -Path 'HKLM:SoftwareMicrosoft.NETFramework' -Key 'Enable64Bit'
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[System.String]
$Path,
[Parameter(Position=1, Mandatory=$True)]
[System.String]
$Key
)
begin {
}
process {
[bool]((Test-Path -Path $Path) -and (Get-ItemProperty -Path $Path -Name $Key -ErrorAction 'SilentlyContinue'))
}
end {
}
}
Dúvidas? Sugestões? Comente!
Até a próxima!

1 Comment
up