0

I have a ps1 file that I run from my Java web application (on the intranet). The ps1 file get the client printers. It works OK on Windows 7 clients, but on Windows 10 clients it doesn't perform OpenRemoteBaseKey().

My Java command is:

String vCommand =  "cmd /c powershell -executionpolicy bypass -file \"" + scriptFilename + "\"  " +vHostName ;
StringBuffer sbInput = new StringBuffer();
StringBuffer sbError = new StringBuffer();

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(vCommand);
proc.getOutputStream().close();
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

String line;
while ((line = bufferedreader.readLine()) != null) {
    sbInput.append(line + "\n");
}

inputstream = proc.getErrorStream();
inputstreamreader = new InputStreamReader(inputstream);
bufferedreader = new BufferedReader(inputstreamreader);
while ((line = bufferedreader.readLine()) != null) {
    sbError.append(line + "\n");
}

and my PowerShell code is:

$Computer = $args[0];
$print = "\Software\Microsoft\Windows NT\CurrentVersion\Devices";
$default = "\Software\Microsoft\Windows NT\CurrentVersion\Windows";
$UsersKey = "SYSTEM\CurrentControlSet\Control\hivelist";
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('localmachine', $Computer)
$HiveList = $Reg.OpenSubKey($UsersKey);
foreach ($sub in $HiveList.GetValueNames()) {
    $tmp =  $HiveList.GetValue($sub);

    if (($HiveList.GetValue($sub) -like "*\Users*" -and $HiveList.GetValue($sub) -like "*\NTUSER.DAT*") -Or ( -Not ($HiveList.GetValue($sub) -like "*Service*") -and $HiveList.GetValue($sub) -like "*\NTUSER.DAT")) {
        if (($HiveList.GetValue($sub) -match "\\Users\\(.*)\\") -Or (-Not ($HiveList.GetValue($sub) -like "*Service*"))) {
            $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('users', $Computer)

            #all printers
            $CurrReg = $Reg.OpenSubKey($sub.Replace("\Registry\User\", "").Replace("\REGISTRY\USER\", "") + $print);

            if($CurrReg) {
                $Printers += @($CurrReg.GetValueNames())
                Write-Output $Printers
            }

            #default
            $CurrReg = $Reg.OpenSubKey($sub.Replace("\Registry\User\", "").Replace("\REGISTRY\USER\", "") + $default);
            if($CurrReg) {
                if (![string]::IsNullOrEmpty($CurrReg.GetValue("Device"))) {
                    $defaultPrinter = $CurrReg.GetValue("Device");

                    if ($defaultPrinter.IndexOf(",") -gt 0) {
                        Write-Output "default :$($defaultPrinter.Substring(0, $defaultPrinter.IndexOf(",")))";
                    } else {
                        Write-Output "default :$($defaultPrinter)";
                    }
                }
            }

            exit;
        }
    }
}

Any idea how to make it work for all clients?

What is causing the problem?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
tamih
  • 93
  • 1
  • 15
  • How does it fail? What error message - _exactly_ - do you get (please edit it into your question as a code block)? Are you certain you have sufficient rights? Are you running the script AS ADMINISTRATOR? – Jeff Zeitlin Apr 02 '18 at 10:05
  • I didn't found the way to print the exact error , but after adding line by line to my ps1 file I fond that it crush on OpenRemoteBaseKey command . I am not sure that I have sufficient rights , what do you recommend me to check ? – tamih Apr 02 '18 at 11:12
  • 2
    Make sure the Remote Registry service is running on the remote machine – EBGreen Apr 02 '18 at 12:17
  • 2
    ... and that the firewall doesn't block access. – Ansgar Wiechers Apr 02 '18 at 12:33

1 Answers1

0

Might be an x64 /x86 issue. Have you tried adding the "Registry64" parameter?

[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$CompName,'Registry64')

cyberjitz
  • 17
  • 1
  • 5