MarsJupiter
MarsJupiter

Reputation: 1

How can I set a bunch of Reg Keys from a array with PowerShell and test before the existing path

    @{
Keys = @(
    @{
        Id    = "DTBI014-IE11"
        Task  = "Turn off Encryption Support must be enabled."
        Path  = "HKLM:\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings"
        Name  = "SecureProtocols"
        Value = 2560
    }
    @{
        Id    = "DTBI015-IE11"
        Task  = "The Internet Explorer warning about certificate address mismatch must be enforced."
        Path  = "HKLM:\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings"
        Name  = "WarnOnBadCertRecving"
        Value = 1
    }
        @{
        Id    = "DTBI018-IE11"
        Task  = "Check for publishers certificate revocation must be enforced."
        Path  = "HKCU:\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing"
        Name  = "State"
        Value = 146432
    }

)

} ForEach ($Key in $Keys) { Write-Output $Task New-ItemProperty -Path $Path -Name $Name --PropertyType Dword -Value $Value -Force }

Upvotes: 0

Views: 225

Answers (1)

Theo
Theo

Reputation: 61198

As commented, you are using undefined variables like $Path, $Name etc. instead of the properties .Path, .Name etc.

To test if the key exists before trying to add a value, you can use the Test-Path cmdlet:

$Keys = 
    @{
        Id    = "DTBI014-IE11"
        Task  = "Turn off Encryption Support must be enabled."
        Path  = "HKLM:\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings"
        Name  = "SecureProtocols"
        Value = 2560
    },
    @{
        Id    = "DTBI015-IE11"
        Task  = "The Internet Explorer warning about certificate address mismatch must be enforced."
        Path  = "HKLM:\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings"
        Name  = "WarnOnBadCertRecving"
        Value = 1
    },
        @{
        Id    = "DTBI018-IE11"
        Task  = "Check for publishers certificate revocation must be enforced."
        Path  = "HKCU:\Software\Microsoft\Windows\CurrentVersion\WinTrust\Trust Providers\Software Publishing"
        Name  = "State"
        Value = 146432
    }

foreach ($item in $Keys) {
    Write-Output $item.Task
    if (!(Test-Path $item.Path)) {
        # if the key does not already exist, create it
        New-Item -Path $item.Path -Force | Out-Null
    }
    # now add the new property
    New-ItemProperty -Path $item.Path -Name $item.Name -PropertyType Dword -Value $item.Value -Force -WhatIf  | Out-Null
}

Upvotes: 0

Related Questions