简体   繁体   中英

Powershell - Append Variable to an Array using For Each

I'm currently trying to add a registry key to mutiple registry folders. However becasue you can't wildcard registry keys via group policy I'm using a ps1 script to add these keys. Essentially I would like to add a REG_Binary key with the hex value of 00 00 00 00 in this location: HKCU:\\SOFTWARE\\Microsoft\\Office\\15.0\\Outlook\\Profiles\\name of outlook profile\\c02ebc5353d9cd11975200aa004ae40e.

I have the code that adds this key on one profile using one line of PS. This is:

$path = "HKCU:\SOFTWARE\Microsoft\Office\15.0\Outlook\Profiles\Name of Outlook Profile\c02ebc5353d9cd11975200aa004ae40e"

New-ItemProperty -Path $path -Name 00030354 -PropertyType Binary -Value ([byte[]](0x00,0x00,0x00,0x00)) -Force

This works perfectly, however I need to do this on all folders in the profiles directory. I can find out all the profile names in this string via this command:

$Profilelist = get-childItem "HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles\*" | select name

How can I add the value "\\c02ebc5353d9cd11975200aa004ae40e" to each line of this array/list?

After adding this I will be able to use a for each command to add the key.

If anyone knows a better way I'm open to other ideas!

Thanks in advance! :)

IMO具有计算属性的最简单方法是:

$Profilelist = get-childItem "HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles\*" | select @{n='extendedname'; e={'{0}\c02ebc5353d9cd11975200aa004ae40e' -f $_.Name.Replace('HKEY_CURRENT_USER\', 'HKCU:\'}}

Try:

Get-ChildItem -Path "HKCU:\SOFTWARE\Microsoft\Office\15.0\Outlook\Profiles" | ForEach-Object {
    $path = Join-Path $_.Name "c02ebc5353d9cd11975200aa004ae40e"
    #Create key if missing
    if(-not (Test-Path $path)) { New-Item -ItemType RegistryKey -Path $path }
    New-ItemProperty -Path $path -Name 00030354 -PropertyType Binary -Value ([byte[]](0x00,0x00,0x00,0x00)) -Force
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM