简体   繁体   English

调用powershell脚本后,批处理文件不会继续

[英]Batch file wont continue after call up a powershell script

I have a situation. 我有一个情况。 I'm installing network printer on remote desktops using a batch file that calls up a Powershell script, but after printer is installed on the remote desktop, Powershell returns nothing - so my batch file wont be able to continue...I must manually hit ENTER twice to make it echo DONE and run the rest of the scripts. 我正在使用调用Powershell脚本的批处理文件在远程桌面上安装网络打印机,但是在远程桌面上安装打印机之后,Powershell什么也没有返回 - 所以我的批处理文件将无法继续...我必须手动命中输入两次以使其echo DONE并运行其余脚本。 Any idea why? 知道为什么吗?

@echo off

Copy /Y "\\AddPrinter\AddPrinter.ps1" "C:\scripts" 

powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1" 

Echo DONE

Del /F /Q "C:\ntfs3\scripts\AddPrinter.ps1"

This is my powershell script, I even put a exit in the end of script, but powershell on remote pc just wont exit.....so it will not return to batch 这是我的powershell脚本,我甚至在脚本的末尾放了一个exit ,但是远程PC上的powershell只是不会退出.....所以它不会返回批处理

$PrinterPath = "\\server-01\Printer0101"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)
exit

BTW, I m using psexec to call up this batch file with a txt file containing all pc names. 顺便说一句,我使用psexec调用这个批处理文件,其中包含一个包含所有pc名称的txt文件。

psexec @%1 -u admin -p xxxxxx -c -f "C:\AAA\AddPrinter.bat"

I did some research, some one said need to use call with %1 > null to make Powershell return to batch. 我做了一些研究,有人说需要使用%1 > null call来使Powershell返回批处理。 But it does not work either. 但它也不起作用。

Call powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1"  %1 > null

I could only use batch command to install printer, but after installation, the new printer always setup as a default printer. 我只能使用批处理命令安装打印机,但安装后,新打印机始终设置为默认打印机。 but if use powershell to install it, it wont change default printer on remote desktop. 但如果使用powershell安装它,它不会更改远程桌面上的默认打印机。

rundll32 printui.dll PrintUIEntry /ga /n\\server-01\printer0101

Hint please. 请提示。

I have regularly seen problems calling PowerShell through psexec.exe . 我经常看到通过psexec.exe调用PowerShell的问题。 I would recommend ditching psexec.exe and configuring PowerShell Remoting instead. 我建议放弃psexec.exe并配置PowerShell Remoting。 Then, you can deploy your script using the Invoke-Command command. 然后,您可以使用Invoke-Command命令部署脚本。

I could also suggest for DOS like commands to use invoke-expression . 我也建议使用DOS命令来使用invoke-expression PowerShell will process the command that you are passing as it is. PowerShell将按原样处理您传递的命令。 The great is that you will be able to pass also variables 伟大的是你将能够传递变量

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

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