JPowerShell以管理员身份运行命令

时间:2018-12-14 12:50:07

标签: java powershell

我有一个JavaFx应用程序,该应用程序应清理PC的Temp,Prefetch并清理浏览器。浏览器部分工作正常,而某些临时部分工作正常,但是预取部分由于无法通过Powershell访问而无法正常工作。 该代码是我正在运行的代码,当我使用-whatif进行测试时很好,但是当然,当我删除-whatif时,由于访问Windows / Prefetch和appdata / Temp所需的管理员权限而拒绝了实际命令。 >

try (PowerShell powerShell = PowerShell.openSession()) {
                    PowerShellResponse response;
                    response = powerShell.executeCommand("Set-Location “C:\\Windows\\Temp”");
                    response = powerShell.executeCommand("Remove-Item * -recurse -force");
                    response = powerShell.executeCommand("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1");
                    response = powerShell.executeCommand("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8");
                    response = powerShell.executeCommand("Set-Location “C:\\Windows\\Prefetch”");
                    response = powerShell.executeCommand("Remove-Item * -recurse -force");
                    response = powerShell.executeCommand("Set-Location “C:\\Documents and Settings”");
                    response = powerShell.executeCommand("Remove-Item * -recurse -force");
                    response = powerShell.executeCommand("Remove-Item -Path $env:TEMP -Recurse -Force");
                    response = powerShell.executeCommand("Set-Location \"$env:LOCALAPPDATA\\Google\\Chrome\\User Data\\Default\\\"");
                    response = powerShell.executeCommand("taskkill /F /IM \"chrome.exe\"");
                    response = powerShell.executeCommand("Remove-Item Cache -recurse -force");
                    response = powerShell.executeCommand("Remove-Item History -recurse -force");
                } catch (PowerShellNotAvailableException ex) {
                }

有什么想法如何使用JPowerShell或可能在此处起作用的任何其他方法获得管理员权限?

0 个答案:

没有答案