这是我目前做的文件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:
启动CMD -nonewwindow工作
启动CMD -ver runas工作
然而启动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
谁能帮我解决开始cmd -nonewwindow -ver runas问题或输入多行代码到一个新的升高的命令提示符只有一个文件,请?
# # # < blockquote >
谁能帮我解决开始cmd -nonewwindow -动词runas问题
不幸的是,没有解决方案:Windows从根本上不允许你在一个非提升的进程的控制台窗口中直接运行一个提升的进程(以管理员请求的方式运行,使用- verb RunAs)——这就是Start-Process语法上阻止- nonewwindow和- verb RunAs组合的原因。
或者只需要一个文件就可以在一个新的命令提示符中输入多行代码?
虽然有一个难以维护的解决方案:
你可以将你的第二个批处理文件(你想要消除的文件)的行传递到cmd /c,用&:
注:为了方便无副作用的实验原件diskpart
command was replaced with findstr -n .
which merely Command被替换为findstr -n .
which merely prints the l它仅仅打印通过stdin接收到的行,这些行在它们的行号之前。
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'"
没有空格字符。在每个&之前是故意的,因为在echo命令中尾随空格是重要的,即它成为输出的一部分;不过,放置一个空格字符应该没问题。在每个&之后(如果前面的命令忽略后面的空格,则在前面)。
一个更好的解决方案是从你的批处理文件中创建一个临时的辅助批处理文件,将其路径传递给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%"