我有一个ps1文件,我从我的Java Web应用程序(在Intranet上)运行。
ps1文件获取客户端打印机。
它在Windows 7客户端上运行正常,但在Windows 10客户端上它不执行OpenRemoteBaseKey()
。
我的Java命令是:
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");
}
我的PowerShell代码是:
$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;
}
}
}
知道如何让它适用于所有客户吗?
导致问题的原因是什么?
答案 0 :(得分:0)
可能是x64 / x86问题。您是否尝试添加“ Registry64”参数?
[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$CompName,'Registry64')