简体   繁体   English

Powershell-使用For Each将变量附加到阵列

[英]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. 但是,因为您不能通过组策略对注册表项进行通配,所以我正在使用ps1脚本添加这些项。 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. 本质上,我想在此位置添加一个十六进制值为00 00 00 00的REG_Binary密钥:HKCU:\\ SOFTWARE \\ Microsoft \\ Office \\ 15.0 \\ Outlook \\ Profiles \\ Outlook配置文件的名称\\ c02ebc5353d9cd11975200aa004ae40e。

I have the code that adds this key on one profile using one line of PS. 我有使用一行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? 如何将值“ \\ c02ebc5353d9cd11975200aa004ae40e”添加到此数组/列表的每一行?

After adding this I will be able to use a for each command to add the key. 添加后,我将能够对每个命令使用a来添加密钥。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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