AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1599726
Accepted
bluesquare
bluesquare
Asked: 2020-11-05 11:03:24 +0800 CST2020-11-05 11:03:24 +0800 CST 2020-11-05 11:03:24 +0800 CST

Powershell -command 和 -argumentlist 有什么区别?

  • 772

所以我想把这个文件复制到程序文件中,它需要像这样(必须右键单击,以管理员身份运行):

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" 

所以我猜 argumentlist 仅由 Start-Process 使用。

那么什么是powershell -command与Start-Process powershell -argumentlist那么当它必须运行多参数命令时为什么会遇到 -Verb runas 问题,例如:

Copy-Item A -Destination B

?

注意:我认为是时候买一本 Powershell 书了。

powershell command-line
  • 1 1 个回答
  • 5220 Views

1 个回答

  • Voted
  1. Best Answer
    postanote
    2020-11-06T10:37:02+08:002020-11-06T10:37:02+08:00

    虽然得到一本书会给你一些东西,但首先要访问帮助文件,因为它们是免费的并且就在你面前。 ;-}

    # 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}"
    

    Thye 可以组合,例如,如果您在 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}
    
    }
    
    • 2

相关问题

  • 将前景颜色添加到 Powershell 配置文件?

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve