Powershell -command 和 -argumentlist 有什麼不同?

Powershell -command 和 -argumentlist 有什麼不同?

所以我想將此文件複製到程式文件中,它需要像這樣(必須右鍵單擊,以管理員身份運行):

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 執行命令是兩種不同的事情。每個都有其引用細節。由於語法不正確(包括所需的引用),您會收到這些類型的錯誤。

因此,對於您的用例,啟動進程將是這樣的:

$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}

}

相關內容