简体   繁体   English

脚本在 Visual Studio 中工作,但从批处理文件运行时不工作

[英]script working in Visual studio but not when running from Batch file

I have a script which supposed to run powershell with different credentials and then in that session supposed to goto network path and install a file:我有一个脚本,它应该使用不同的凭据运行 powershell,然后在 session 中应该转到网络路径并安装文件:

$path1 = "\\192.168.10.10\bucket\test.exe"
Start-Process powershell -Credential $creds -ArgumentList "-Command &{Start-Process $path1 -Verb RunAs}"

looks like no problem running this from the ISE but when i save the ps file and tried to call it from batch file i get the folowing error:从 ISE 运行它看起来没有问题,但是当我保存 ps 文件并尝试从批处理文件中调用它时,我得到以下错误: 在此处输入图像描述

What could be the reason for that?这可能是什么原因?

When you use Start-Process with the -Credential parameter, whatever happens to be the caller's current directory matters:当您使用带有-Credential参数的Start-Process时,调用者的当前目录很重要:

If the target user identified by the -Credential argument happens not have the required permissions to access the caller 's current directory, you'll get the error you saw.如果-Credential参数标识的目标用户碰巧没有访问调用者当前目录所需的权限,您将收到您看到的错误。

The solution:解决方案:

  • Use the -WorkingDirectory parameter to specify a current (working) directory for the new process that the target user is allowed access to.使用-WorkingDirectory参数允许目标用户访问的新进程指定当前(工作)目录。

  • Alternatively, switch to such a directory in the caller 's context first.或者,首先在调用者的上下文中切换到这样的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM