多个输入到新提示和 Powershell -运行 as 和 -nonewwindow 问题

Multiple inputs into new prompt & Powershell -run as and -nonewwindow issue

这是我目前所做的,文件 1:

powershell.exe -command "Start-Process cmd -ArgumentList '/c cd C:\ && DiskZero.cmd'-Verb runas"

和文件 2“DiskZero.cmd”:

@echo off
(echo rescan
echo sel disk 1
echo cle all
echo cre part prim
echo for fs=ntfs quick label=Intenso
echo assign letter=E
)  | diskpart
pause

它按预期工作,但是,有两个文件,我想做的是让它只有一个文件。

我无法找到如何仅使用一个脚本将多行代码输入到新的提升命令提示符中,因此我尝试使用 powershell 来完成:

start cmd -nonewwindow 有效

start cmd -ver runas 有效

但是 start cmd -nonewwindow -ver runas 不起作用

我希望在 powershell 中做的是:

start cmd -nonewwindow -ver runas
@echo off
(echo rescan
echo sel disk 1
echo cle all
echo cre part prim
echo for fs=ntfs quick label=Intenso
echo assign letter=E
)  | diskpart
pause

任何人都可以帮我解决 start cmd -nonewwindow -ver runas 问题 请将多行代码输入到一个只有一个文件的新提升的命令提示符中,好吗?

Can anyone help me solve the start cmd -nonewwindow -verb runas issue

不幸的是,没有解决方案:Windows根本不允许您运行 提升 进程(运行 作为管理员,使用 -Verb RunAs 请求)直接在 非提升的 进程的控制台 window - 这就是为什么 Start-Process 在语法上 阻止将 -NoNewWindow-Verb RunAs.

组合

OR input multiple lines of code into a new elevated command prompt with only one file, please?

虽然有解决方案,但难以维护:

您可以将第二个批处理文件的行(您要删除的行)传递给cmd /c 行,加入&:

  • 注意:为了促进无副作用的实验,原来的 diskpart 命令被替换为 findstr -n .,它仅 打印 通过标准输入接收的行, 前面是他们的行号。
powershell.exe -command "Start-Process -Verb RunAs cmd '/c cd C:\ && (echo rescan&echo sel disk 1&echo cle all&echo cre part prim&echo for fs=ntfs quick label=Intenso&echo assign letter=E) | findstr -n .&pause'"

即没有space字符。在每个 & 之前是故意的,因为 echo 命令中的尾随白色 space 很重要,即它成为输出的一部分;但是,放置一个 space 字符应该没问题。 after each &(以及之前,如果前面的命令忽略尾随白色space)。

更好的解决方案是从您的批处理文件创建一个临时辅助批处理文件,将其路径传递给 PowerShell 命令,然后之后删除它:

@echo off

:: Determine the path for a temporary batch file...
:: Note: %~snx0 uses the short (8.3) name of the batch file, so as
::       to ensure that the temp. file path has no spaces, which 
::       obviates the need for complex double-quoting later.
set "tmpBatchFile=%TEMP%\~%~snx0"

:: ... and fill it with the desired commands.
:: Note how metacharacters - ( ) | ... - must be ^-escaped.
(
echo @echo off
echo ^(echo rescan
echo echo sel disk 1
echo echo cle all
echo echo cre part prim
echo echo for fs=ntfs quick label=Intenso
echo echo assign letter=E
echo ^) ^| findstr -n .
echo pause
) > "%tmpBatchFile%"

:: Now you can let the elevated cmd.exe process that PowerShell launches
:: execute the temp. batch file.
:: Note: -Wait ensures that the PowerShell call blocks until the elevated 
::       cmd.exe window closes.
powershell.exe -command "Start-Process -Wait -Verb RunAs cmd '/c cd C:\ & %tmpBatchFile%'"

:: Delete the temp. batch file.
:: Note: If you do NOT use -Wait above, you'll have to defer deleting
::       the batch file until after the elevated cmd.exe window closes,
::       which you'll have to do manually.
del "%tmpBatchFile%"