I want to install browser add-ons using local group policy. Here, for example, for Firefox the add-on Zotero Connector. For this I use the Powershell module PolicyFileEditor.
Write-host "Trusting PS Gallery"
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted -ErrorAction Stop
Write-Host "Installing PolicyFileEditor"
Install-Module -Name PolicyFileEditor -Scope CurrentUser -ErrorAction Stop
Write-Host "Installing Firefox Add-On (set LGPO)"
$MachineDir = "$env:windir\system32\GroupPolicy\Machine\registry.pol"
$RegPath = 'SOFTWARE\Policies\Mozilla\Firefox\Extensions\Install'
$RegData = 'https://download.zotero.org/connector/firefox/release/Zotero_Connector-5.0.102.xpi'
$RegName = '1'
$RegType = 'ExpandString'
Set-PolicyFileEntry -Path $MachineDir -Key $RegPath -ValueName $RegName -Data $RegData -Type $RegType -ErrorAction Stop
So far so good. But now I want to avoid that a potentially already existing value name (= an already installed add-on) is overwritten. Therefore I don't want $RegName = '1'
, but the next not existing number as name (string), e.g. $RegName = '2'
or $RegName = '3
', if 1 and 2 are already available as value names etc. etc.
Currently I use the following:
$RegName = '1'
if (Get-PolicyFileEntry $MachineDir -Key $RegPath -ValueName $RegName) { $RegName = '2' }
if (Get-PolicyFileEntry $MachineDir -Key $RegPath -ValueName $RegName) { $RegName = '3' }
if (Get-PolicyFileEntry $MachineDir -Key $RegPath -ValueName $RegName) { $RegName = '4' }
With this option I just assume that no more than 4 add-ons are installed.
Isn't there a nicer and more purposeful solution (e.g. with RegEx), which I can pass to the variable $RegName
?
Somthing like: $RegName = "Take the next available (not existing) number as name (string)"