PowerShell New-Item属性仅在属性不存在时运行



我正在尝试启用允许活动内容在我的计算机文件中运行使用powershell脚本。我发现,从未启用此设置的pc,从未创建此设置的注册表。我想要的脚本做的是检查它是否存在于注册表中,如果它不在那里…然后去创造它。如果它确实存在,那么只需启用它。

这是我的尝试…

创建

New-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord

启用

Set-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord

使用上面的代码…我试着使用IF Else语句,但似乎不能得到它的权利…

If(get-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet 
ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe){
New-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord -Force | Out-Null
Write-Host 'added Registry value' -f red
}
Else{ 
set-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0"
Write-Host "Active Content is set"

相反,我得到这个错误以及注册表的创建,但不是正确的类型…这是我在DWord中需要的。

get-ItemProperty : Property iexplore.exe does not exist at path HKEY_CURRENT_USERSOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN.
At line:2 char:4
+ If(get-ItemProperty -path "HKCU:SOFTWAREMicrosoftInternet Explorer ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (iexplore.exe:String) [Get-ItemProperty], PSArgumentException
+ FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand

Active Content is set

任何形式的帮助都是非常感激的

看起来你是倒着做的?你做的是If(get-ItemProperty... THEN New-Item ELSE SET,但这应该是相反的。这里有一些应该可以工作的东西

$RegItem = @{
Path = 'HKCU:SOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_LOCALMACHINE_LOCKDOWN'
Name = 'iexplore.exe'
}
# Create path if missing
$Path = Get-Item -Path $RegItem.Path -ErrorAction SilentlyContinue
if ($null -eq $Path) { New-Item -Path $RegItem.Path }

if ($null -eq (Get-ItemProperty @RegItem -ErrorAction SilentlyContinue)) {
New-ItemProperty @RegItem -Value "0" -PropertyType DWord -Force | Out-Null
Write-Host 'added Registry value' -f red
} else {
set-ItemProperty @RegItem -Value "0"
Write-Host "Active Content is set"
}

额外注意

我添加了一个部分来创建FEATURE_LOCALMACHINE_LOCKDOWN,如果它缺失,还通过删除路径/名称的所有重复实例来清理代码。

最新更新