hey everyone,
maybe this is not relevant anymore, but I had the same problem the last months, but I could figure out the problem and could also solve it.
First of all, thanks for this discussion and also the script on top, this was really helpful.
I compared the input from the script with the value which I get if I enter the data manually in the port configuration of the IPP printer.
My Test data:
UserName: printer
Passowrd: password
--- The Byte array for "printer" and "password" (my input value) is:
112 0 114 0 105 0 110 0 116 0 101 0 114 0
----
112 0 97 0 115 0 115 0 119 0 111 0 114 0 100 0
--- The Byte array for "printer" and "password" (the value coming from the registry after "[Security.Cryptography.ProtectedData]::Unprotect"
112 0 114 0 105 0 110 0 116 0 101 0 114 0 0 0
----
112 0 97 0 115 0 115 0 119 0 111 0 114 0 100 0 0 0
The differences are the appending "zero".
Then I have changed my script to append the zero
**************************************
$stringBytes = [Text.Encoding]::Unicode.GetBytes($username)
$addBytes = [System.Byte[]]::CreateInstance([System.Byte],2);
$newString = [byte[]]::new($stringBytes.Length + $addBytes.Length)
$stringBytes.CopyTo($newString, 0)
$addBytes.CopyTo($newString, $stringBytes.Length)
**************************************
The "$newString" variable looks now like the one coming from the registry for the username. You have to to this for the password as well.
If I now use "[Security.Cryptography.ProtectedData]::Protect" function and change the registry attribute, then it works.
It tooks me a lot of time. But maybe other need this information as well, so I want to share it here.
- my script - It has not the best style. But it works.
***** script snippet *****
Add-Type -AssemblyName System.Security
class Helper {
static [byte[]]CreateByteString([string]$inputString) {
$stringBytes = [Text.Encoding]::Unicode.GetBytes( $inputString )
$addBytes = [System.Byte[]]::CreateInstance([System.Byte],2);
$newString = [byte[]]::new($stringBytes.Length + $addBytes.Length)
$stringBytes.CopyTo($newString, 0)
$addBytes.CopyTo($newString, $stringBytes.Length)
return $newString
}
}
$printerFullName = "
https://myprinter.my.domain.eu"
$username = "printer"
$password = "password"
$stringBytesUser = [Helper]::CreateByteString($username);
$stringBytesPassword = [Helper]::CreateByteString($password);
$scope = [Security.Cryptography.DataProtectionScope]::CurrentUser
$regValueUser = [Security.Cryptography.ProtectedData]::Protect( $stringBytesUser, $null, $scope )
$regValuePassword = [Security.Cryptography.ProtectedData]::Protect( $stringBytesPassword, $null, $scope )
$versionCheck = Get-Item -LiteralPath "HKCU:\Printers\Inetnet Print Provider" -erroraction 'silentlycontinue';
if($versionCheck) {
$RegInternetPath = "HKCU:\Printers\Inetnet Print Provider\";
$RegPortsPath = "HKCU:\Printers\Inetnet Print Provider\Ports"
$RegPrinterpath = "HKCU:\Printers\Inetnet Print Provider\Ports\"+$printerFullName
} else {
$RegInternetPath = "HKCU:\Printers\Internet Print Provider\";
$RegPortsPath = "HKCU:\Printers\Internet Print Provider\Ports"
$RegPrinterpath = "HKCU:\Printers\Internet Print Provider\Ports\"+$printerFullName
}
$KeyPortExists = Get-Item -LiteralPath $RegPortsPath -erroraction 'silentlycontinue';
if(!($KeyPortExists)) {
New-Item -Path $RegPortsPath;
}
$KeyPrinterExists = Get-Item -LiteralPath $RegPrinterpath -erroraction 'silentlycontinue';
if(!($KeyPrinterExists)) {
$key = (Get-Item -LiteralPath $RegInternetPath).OpenSubKey("Ports", $true).CreateSubKey($printerFullName)
}
$PropertyType = "Binary"
New-ItemProperty -Path $RegPrinterpath -Name "UserName" -Value $regValueUser -PropertyType $PropertyType -Force | Out-Null
New-ItemProperty -Path $RegPrinterpath -Name "Password" -Value $regValuePassword -PropertyType $PropertyType -Force | Out-Null
New-ItemProperty -Path $RegPrinterpath -Name "Authentication" -Value "4" -PropertyType "DWord" -Force | Out-Null