我正在尝试使用 Powershell 将值写入 REG_BINARY。如果我提供数据,我可以写入 REG_BINARY,但我需要能够对我想在文件中写入的内容进行编码,以便我可以使用系统和日期变量。下面是我的代码和错误。
如果您取消注释第一个 $data 它将起作用。
function Convert-ToCHexString
{
param ([String] $str)
$ans = ''
[System.Text.Encoding]::ASCII.GetBytes($str) | % { $ans += "0x{0:x2}," -f $_ }
return $ans.Trim(' ',',')
}
$Folder = Convert-ToCHexString Z:\
$Username = Convert-ToCHexString $env:USERNAME
$Filename = Convert-ToCHexString \archive.pst
$key = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\0a0d020000000000c000000000000046"
#$data = 0x5a,0x3a,0x5c,0x61,0x72,0x63,0x68,0x69,0x76,0x65,0x2e,0x70,0x73,0x74
$data = $Folder + "," + $Username + "," + $Filename + ",0x00,0x00,0x00"
Set-ItemProperty -path $key -name "001f0324" -value ([byte[]]($data))
以下是我得到的错误:
无法转换值“0x5a,0x3a,0x5c,0x6a,0x62,0x79,0x65,0x72,0x73,0x5c,0x61,0x72,0x63,0x68,0x69,0x76,0x65,0x2e,0x70,0x73,0x74,0x00,0x00, 0x00" 以键入"System.Byte[]"。错误:“无法转换值”0x5a,0x3a,0x5c,0x6a,0x62,0x79,0x65,0x72,0x73,0x5c,0x61,0x72,0x63,0x68,0x69,0x76,0x65,0x2e,0x70,0x73,0x74,0x00 ,0x00,0x00”以键入“System.Byte”。错误:“其他不可解析的字符位于字符串的末尾。”