Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java内部以其他用户身份调用PowerShell脚本_Java_Powershell - Fatal编程技术网

从Java内部以其他用户身份调用PowerShell脚本

从Java内部以其他用户身份调用PowerShell脚本,java,powershell,Java,Powershell,我有一系列脚本,当从命令行调用时,它们可以按预期工作,但是如果同一用户在同一帐户下运行TomCat作为服务启动进程时,似乎会返回而不执行任何操作 我的问题是,在从Java执行命令期间构建的环境是否有本质上的不同?任何指示都会有帮助 从命令行调用 如果在非提升命令提示符下以cuser1身份登录时发出命令cmd/c:\test\test.bat,我会看到以下日志: HS0\cuser1 called script as HS0\cuser2 Script running as HS0\cuser2

我有一系列脚本,当从命令行调用时,它们可以按预期工作,但是如果同一用户在同一帐户下运行TomCat作为服务
启动进程
时,似乎会返回而不执行任何操作

我的问题是,在从Java执行命令期间构建的环境是否有本质上的不同?任何指示都会有帮助

从命令行调用

如果在非提升命令提示符下以cuser1身份登录时发出命令cmd/c:\test\test.bat
,我会看到以下日志:

HS0\cuser1 called script as HS0\cuser2
Script running as HS0\cuser2
从Java进程调用

如果我在TomCat应用程序中使用运行时.getRuntime().exec()
发出相同的命令,我看不到开关

List<String> cmd = new ArrayList<>();
cmd.add("cmd");
cmd.add("/c");
cmd.add("C:\\test\\test.bat");
final Process process = Runtime.getRuntime().exec(cmd);
...
// Handling the command output, nothing in the output, no error result
测试蝙蝠

test-switch.ps1

test.ps1


因此,您的PowerShell脚本已启动,但
启动过程
不起任何作用?您是否尝试写入另一个文件?能否确认
$currentUser
在两个脚本中都有效?我可以想象,
%path%
在tomcat上下文中不可用,并且找不到Powershell。其他信息:我在jshell中检查了您的脚本,它们工作正常。如果这不是唯一的java8问题(我不这么认为),那么它肯定与tomcat有关,而不是与java本身有关。1) 我写了两个不同的文件,以确保它不是并发问题。2) 我验证了当前用户是在每个脚本的开头设置的。3) 我使用了PowerShell.exe的完整路径。因此,您的PowerShell脚本已启动,但
启动过程
不起任何作用?您是否尝试写入另一个文件?能否确认
$currentUser
在两个脚本中都有效?我可以想象,
%path%
在tomcat上下文中不可用,并且找不到Powershell。其他信息:我在jshell中检查了您的脚本,它们工作正常。如果这不是唯一的java8问题(我不这么认为),那么它肯定与tomcat有关,而不是与java本身有关。1) 我写了两个不同的文件,以确保它不是并发问题。2) 我验证了当前用户是在每个脚本的开头设置的。3) 我使用了PowerShell.exe的完整路径。
c:\test\test.bat => calls a PowerShell script
c:\test\test-switch.ps1 => invokes a PowerShell script as different user
c:\test\test.ps1 => display current user
PowerShell -NoProfile -ExecutionPolicy Bypass -File "c:\test\test-switch.ps1"
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$pwd = ConvertTo-SecureString "password" -asplaintext -force
$cred = New-Object System.Management.Automation.PSCredential ("HS0\cuser2", $pwd)
$args = "-NoProfile -ExecutionPolicy Bypass -File c:\test\test.ps1"
Start-Process Powershell -Credential $cred -ArgumentList $args
"$currentUser called script as HS0\cuser2"  | Out-File 'c:\test\test.log' -Append
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
"Script running at $(Get-Date) as $currentUser"  | Out-File 'c:\test\test.log' -Append