2015-02-10 40 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