Windows 运行Powershell脚本的批处理脚本:闪烁窗口

Windows 运行Powershell脚本的批处理脚本:闪烁窗口,windows,powershell,batch-file,Windows,Powershell,Batch File,系统:Windows 10 Powershell版本:5.1 用途:从批处理文件运行powershell脚本 参数:当从命令调用时,目录、文件名、服务器、用户名、密码都作为包含在“”中的字符串传入,以处理有空间的情况(例如当前有空间的目录) Powershell脚本工作正常,它创建凭据并启动RDP连接,没有任何问题。但是,从批处理文件调用时,Powershell窗口会闪烁并立即关闭。命令提示窗口以管理员身份运行。 我试过使用: -暂停 --noexit -代码来自 单击出现的Powershel

系统:Windows 10
Powershell版本:5.1

用途:从批处理文件运行powershell脚本 参数:当从命令调用时,目录、文件名、服务器、用户名、密码都作为包含在“”中的字符串传入,以处理有空间的情况(例如当前有空间的目录)

Powershell脚本工作正常,它创建凭据并启动RDP连接,没有任何问题。但是,从批处理文件调用时,Powershell窗口会闪烁并立即关闭。命令提示窗口以管理员身份运行。
我试过使用:
-暂停
--noexit
-代码来自

  • 单击出现的Powershell窗口(尝试使用选择功能暂停窗口)。
    这些都不起作用
有关守则是:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";
除此代码外,未对系统进行任何更改,四个语音标记被标记为通过参数所需的标记,可以在评论之前的底部博客帖子链接下找到。 我很有可能错误地使用了它,我是批处理的新手,甚至比powershell更新。批处理脚本尽可能内部化,它们需要能够被Windows的基本安装使用。它们最终将迁移到Windows Server 2008及更高版本

是否需要使用命令提示符执行任何操作以允许其运行Powershell代码? powershell代码是否符合我打算使用它的目的? 在从批处理脚本运行时,除了列出的方法外,是否还有其他方法可以查看错误、日志信息或暂停powershell窗口

任何意见都将不胜感激

编辑:
-无退出变化:
家长电话

PowerShell **-NoExit**  -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";  
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '**-NoExit** -NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";  
嵌套调用

PowerShell **-NoExit**  -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";  
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '**-NoExit** -NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";  
父级和嵌套的

PowerShell **-NoExit**  -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '**-NoExit** -NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}"; 
在批处理脚本中设置的变量为:

  • 设置服务器=%~1 要删除引号,我还尝试使用:

  • 设置服务器=%1
    设置服务器=%server:“=%


我想确保我读的是正确的。您运行了一个cmd,想要启动运行另一个Powershell的Powershell,然后调用您的脚本?老实说,我觉得有更好的方法来实现这一点。您说您正在尝试打开RDP连接,看起来您希望用户通过命令行访问此连接。这正确吗t?
PowerShell-NoProfile-NoExit…
。然后您可能会看到一个错误?请显示a:如何设置所有使用的变量(混淆敏感数据)。
-NoExit
在批处理脚本中(不在脚本块内)。也许
%password%
中的百分号或双引号可能有害?使用
ECHO ON
运行以查看真正执行的命令…@JosefZ在父调用和嵌套调用中都有-NoExit,并且打开ECHO现在允许Powershell窗口保持打开。另外,从ECHO我可以看到它没有传递服务器和password参数正确。我现在就讲这些,谢谢您的帮助!使用
设置“server=%~1”
(注意)。