【问题标题】:Batch file wont continue after call up a powershell script调用powershell脚本后批处理文件不会继续
【发布时间】:2014-04-08 19:09:09
【问题描述】:

我有一个情况。我正在使用调用 Powershell 脚本的批处理文件在远程桌面上安装网络打印机,但是在远程桌面上安装打印机后,Powershell 什么也不返回 - 所以我的批处理文件将无法继续......我必须手动点击输入两次以使其成为echo DONE 并运行其余脚本。知道为什么吗?

@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"

这是我的powershell脚本,我什至在脚本末尾加了exit,但是远程PC上的powershell只是不会退出.....所以它不会返回批处理

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

顺便说一句,我正在使用 psexec 来调用这个批处理文件,其中包含一个包含所有电脑名称的 txt 文件。

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

我做了一些研究,有人说需要使用call%1 > null 使Powershell 返回批处理。但它也不起作用。

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

我只能使用批处理命令安装打印机,但安装后,新打印机总是设置为默认打印机。但是如果使用powershell安装它,它不会改变远程桌面上的默认打印机。

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

请指教。

【问题讨论】:

  • 也许你的 powershell 脚本需要时间来完成它的执行。能粘贴一下powershell文件的内容吗?
  • 我粘贴了powershell脚本,只需15秒即可完成。
  • 如果您从 PowerShell 提示符手动运行 PowerShell 脚本,它会提示输入吗?
  • 不,它只是运行并完成。完全没有提示
  • 它适用于我的批处理文件中的Call。也许将-NoProfile 开关添加到您的powershell.exe 行?您在哪个帐户下运行?

标签: windows batch-file powershell


【解决方案1】:

我经常看到通过 psexec.exe 调用 PowerShell 时遇到问题。我建议放弃 psexec.exe 并改为配置 PowerShell Remoting。然后,您可以使用Invoke-Command 命令部署您的脚本。

【讨论】:

  • 我同意你的看法。我不允许打开PSRemoting的东西……上层说太冒险了……
【解决方案2】:

我还可以建议类似 DOS 的命令使用 invoke-expression。 PowerShell 将按原样处理您传递的命令。很棒的是你也可以传递变量

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2023-03-30
    • 2017-12-14
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    相关资源
    最近更新 更多