首页 > 解决方案 > 尝试通过 bat 文件加载 powershell 脚本时出现“错误:系统找不到指定的文件”

问题描述

TLDR;

我明白了

powershell.exe -executionpolicy bypass -file "c:\folder\psscript.ps1"
ERROR: the system cannot find the file specified.

当我尝试从 bat 文件中调用 .ps1 时,尽管该文件存在于引用的位置。


我们正在部署一个应用程序,其主要要求是桌面快捷方式,用户只需打开而不做任何其他事情(它运行紧急打印作业,因此需要快速且不费力)。

为此,我们构建了一个 powershell 脚本,该脚本在正确的服务器上调用计划任务(调用应用程序),因此一切都已预设并准备就绪。我们的想法是在一个 .bat 文件中调用这个 .ps1,该文件将由桌面快捷方式启动,非常棒,简单。但是,每当我们尝试调用 .ps1 时,我们都会得到;

ERROR: the system cannot find the file specified.

...除了文件存在,因为 atest-path回来了True,你可以用 tab 完成文件。

.bat 文件的内容是您的标准

powershell.exe -executionpolicy bypass -file "c:\folder\psscript.ps1"

我尝试为 powershell.exe、ps1 提供完全限定的目录路径,并在各种本地和网络目录路径中移动它,看看它是否有所作为……不。

直接在 powershell 中打开或运行 ps1 工作得很好。但是我们需要从桌面快捷方式中“单击并运行”。已绝育的 ps1 脚本如下(脚本可访问 PW.txt 文件);

#$key is to decrypt a stored Password for secure credential passing
$key = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
$password = get-content "\\full\network\location\PW.txt" | ConvertTo-SecureString -key $key
$securecredentials = new-object -typename System.Management.Automation.PSCredential -ArgumentList "domain\adminuser", $password
# Then create a remote session on server job is set up to run on
$session = new-pssession -computername SERVER -credential $securecredentials
Enter-PSSession -Session $session
schtasks /run /TN "Print Job"
#Then to tidy things up we remove sessions and variables from locally run script
Exit-PSSession
Remove-PSSession -ComputerName SERVER
Clear-Variable password
Clear-Variable key
Clear-Variable session

在这个问题上挠头,感谢帮助!

标签: powershellcmd

解决方案


推荐阅读