尝试通过bat文件加载Powershell脚本时出现“错误:系统找不到指定的文件”

时间:2019-05-16 11:13:48

标签: powershell cmd

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.

...除了文件存在之外,因为test-path会以True的形式返回,您可以使用制表符完整地标记该文件。

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

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

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

直接在Powershell中打开或运行ps1绝对可以。但是我们需要从桌面快捷方式中“单击并单击”。下面是Neutered 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

抓着这个头,不胜感激!

0 个答案:

没有答案