所以我只想将此文件复制到 Program Files 中,它需要像这样(必须右键单击,以管理员身份运行):
Copy-Item \\Poplar\SuperSound -Destination 'C:\Program Files\' -Force -Recurse
但我在 powershell 脚本中需要它。
我通常采用以下方式提升:
powershell -command "install_bananas.bat" -Verb runas
但是当我跑步时:
powershell -command "Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse" -Verb runas
...出现了一个错误:
Copy-Item : A positional parameter cannot be found that accepts argument 'runas'.
因此我必须将 -argumentlist 与 Start-Process 结合使用:
Start-Process powershell -Verb runas -argumentlist "Copy-Item \\Poplar\SuperSound -Destination 'C:\Program Files\' -Force -Recurse"
因此我猜测参数列表仅被 Start-Process 使用。
那么什么是powershell -命令相对启动进程 powershell -argumentlist那么为什么当它必须运行如下多参数命令时会遇到 -Verb runas 的问题:
Copy-Item A -Destination B
?
注意:我想现在是时候买一本 Powershell 书了。
答案1
虽然买书能给你带来一些东西,首先查看帮助文件因为它们是免费的并且就在你面前。 ;-}
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Start-Process).Parameters
(Get-Command -Name Start-Process).Parameters.Keys
# Results
<#
FilePath
ArgumentList
Credential
WorkingDirectory
LoadUserProfile
NoNewWindow
PassThru
RedirectStandardError
RedirectStandardInput
RedirectStandardOutput
Verb
WindowStyle
Wait
UseNewEnvironment
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Start-Process -Examples
# Results
<#
Start-Process -FilePath "sort.exe"
Start-Process -FilePath "myfile.txt" -WorkingDirectory "C:\PS-Test" -Verb Print
Start-Process -FilePath "Sort.exe" -RedirectStandardInput "Testsort.txt" -RedirectStandardOutput "Sorted.txt" -RedirectStandardError
Start-Process -FilePath "notepad" -Wait -WindowStyle Maximized
Start-Process -FilePath "powershell" -Verb runAs
$startExe = New-Object System.Diagnostics.ProcessStartInfo -Args PowerShell.exe
$startExe.verbs
Start-Process -FilePath "powershell.exe" -Verb open
Start-Process -FilePath "powershell.exe" -Verb runas
#>
Get-help -Name Start-Process -Full
Get-help -Name Start-Process -Online
powershell /?
# Results
<#
PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
[-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
[-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
[-ConfigurationName <string>]
[-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
...
-Command
Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.
If the value of Command is "-", the command text is read from standard
input.
If the value of Command is a script block, the script block must be enclosed
in braces ({}). You can specify a script block only when running PowerShell.exe
in Windows PowerShell. The results of the script block are returned to the
parent shell as deserialized XML objects, not live objects.
If the value of Command is a string, Command must be the last parameter
in the command , because any characters typed after the command are
interpreted as the command arguments.
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
...
#>
启动进程(Microsoft.PowerShell.Management ... https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7
参数
-ArgumentList 指定此 cmdlet 启动进程时要使用的参数或参数值。参数可以作为单个字符串接受,其中参数以空格分隔,也可以作为字符串数组接受,其中字符串以逗号分隔。
如果参数或参数值包含空格,则需要用转义的双引号括起来。有关更多信息,请参阅 about_Quoting_Rules。
通过 cmdlet 或 powershell.exe 运行命令是两码事。每种方式都有自己的引号细节。您会收到这些类型的错误,因为语法不正确,包括需要引号。
因此,对于您的用例,Start-Process 将是这样的:
$ConsoleCommand = "Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse"
Start-Process powershell -ArgumentList '-NoExit',"-Command &{ $ConsoleCommand }"
对于 PowerShell.exe,如下所示:
PowerShell -Command {Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse}
或这个
PowerShell -Command "& {Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse}"
它们可以组合起来,比如说,如果你在 ISE/VScode 中,并且想要在 ISE/VSCode 中向新实例发出命令,那么,就像这样:
Function Start-ConsoleCommand
{
[CmdletBinding(SupportsShouldProcess)]
[Alias('scc')]
Param
(
[string]$ConsoleCommand,
[switch]$PoSHCore
)
If ($PoSHCore)
{Start-Process pwsh -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -Wait}
Else
{Start-Process powershell -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -Wait}
}