2015-02-10 87 views
0

我一直在努力如何在2008 R2中的打印机对象上设置安全性。在2012年的机器上超级棒,并且想在2008 R2上做类似的事情,但是失败了。通过PowerShell在注册表中设置打印机安全二进制密钥

我写了一个函数来获取该注册表的值,然后是一个辅助函数来设置不同打印机上的值,但不接受该值。

已经预期手动设置打印机上的设置值以根据需要获取权限,然后从中读取并设置为少数其他新添加的打印机。

它回应说明以下错误。

"The type of the value object did not match the specified RegistryValueKind or the object could not be properly converted." 

这是我的测试代码,我摸索着。

$ComputerName = "TESTSERVER01" 

Function Get-RegistryString { 
Param(
    [string]$ComputerName, 
    [string]$KeyPath, 
    [string]$KeyName, 
    [string]$KeyValue 
    ) 

$KeyValueType = [Microsoft.Win32.RegistryValueKind]::String 

try { 
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $ComputerName) 
    $regKey = $reg.OpenSubKey($KeyPath, $True) 
    $regKey.GetValue($KeyName) 

    } catch { 
     Write-Host $_.Exception.Message 
     $error.Clear() 
     return $false 
    } 
} 

Function Set-RegistryBinary { 
Param(
    [string]$ComputerName, 
    [string]$KeyPath, 
    [string]$KeyName, 
    [string]$KeyValue 
    ) 

$KeyValueType = [Microsoft.Win32.RegistryValueKind]::Binary 

try { 
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $ComputerName) 
    $regKey = $reg.OpenSubKey($KeyPath, $True) 
    $regKey.SetValue($KeyName, $KeyValue, $KeyValueType) 

    } catch { 
     Write-Host $_.Exception.Message 
     $error.Clear() 
     return $false 
    } 
} 


$SecKey = Get-RegistryString -ComputerName $ComputerName -KeyPath "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\W-TEST01" -KeyName "Security" 

Set-RegistryBinary -ComputerName $ComputerName -KeyPath "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\W-TEST02" -KeyName "Security" -KeyValue $SecKey 

回答

1

您的问题是简单的行[string]$KeyValue。您正在将一个字节数组转换为一个字符串,该字符串会破坏下一步的数据。我需要做的就是去除演员阵容。也可以将剧组更改为[byte[]]$KeyValue,我认为它也可以。

Function Set-RegistryBinary { 
Param(
    [string]$ComputerName, 
    [string]$KeyPath, 
    [string]$KeyName, 
    [byte[]]$KeyValue$KeyValue 
    ) 

你可以在这里看到一个例子。首先我创建一个字节数组。然后使用相同的数组结构将其转换为字符串。

PS C:\Users\Cameron> [byte[]](1,134,233,5) 
1 
134 
233 
5 

PS C:\Users\Cameron> [string]([byte[]](1,134,233,5)) 
1 134 233 5 

铸造任何数组为一个字符串会做类似的事情以上向片段。

+0

谢谢! - 良好的响应和示例帮助拼出来。我实际上不得不将它作为一个字节数组来执行,只是删除了字符串投射失败。 – ssaviers 2015-02-10 04:54:49