代码之家  ›  专栏  ›  技术社区  ›  Ωmega

正在退出和(&)登录Powershell

  •  1
  • Ωmega  · 技术社区  · 8 年前

    我需要使用 \r\n 的内部 ffmpeg --header 参数这适用于Unix,但不适用于Windows命令提示符。所以我想用 powershell

    powershell c:\ffmpeg -headers 'User-Agent: user-agent'"`r`n"'Cookies: my-cookie' ...
    

    我知道我必须使用 'string' 使用特殊字符时,以及

    "`r`n"
    

    作为我的 \r\n 分隔符。

    我还测试了我可以将它们混合在一起,比如 'this '"is "'text' 得到 this is text

    但是,如果我的字符串(cookie或用户代理)包含 & 角色,它失败了。

    例子: powershell c:\ffmpeg -headers 'Cookies: param=a=1&b=2; session=123'

    我怎样才能“逃脱” & 一行中的字符?


    CMD接受这些示例(某些部分被屏蔽),但它们不起作用

    powershell -c "c:\ffplay -user_agent ""Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"" -headers ""Cookie: nlqptid=h=676886edeea5bae904b0cf53daec8038.1519940538670&hdnea=exp=1519940598~acl=/*~hmac=C0E019502B060D23AB02BB157FCFFC72404500770A2CE5B00789A84AAEFBD77F&nltid=xxxxxxxxxx&nltdt=0&uid=744826&csid=xxxxxxxxxx; hdntl=exp=1520026938~acl=%2f*~hmac=952689e6de57a2a201ddc1d4c0794962fdc886ea48cf41494a42e787ef923bf9`r`n"" -i ""https://xxxxxxxxxx.net/nlds_vod/xxxxxxxxxx/vod/2018/02/28/cd264e10-dd54-3de2-df90-4c1cac104dcb/v1/stream/cd264e10-dd54-3de2-df90-4c1cac104dcb_1_4500_pc.mp4.m3u8"""
    

    或

    powershell -c "c:\ffplay -headers ""User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0`r`nCookie: nlqptid=h=676886edeea5bae904b0cf53daec8038.1519940538670&hdnea=exp=1519940598~acl=/*~hmac=C0E019502B060D23AB02BB157FCFFC72404500770A2CE5B00789A84AAEFBD77F&nltid=xxxxxxxxxx&nltdt=0&uid=744826&csid=xxxxxxxxxx; hdntl=exp=1520026938~acl=%2f*~hmac=952689e6de57a2a201ddc1d4c0794962fdc886ea48cf41494a42e787ef923bf9`r`n"" -i ""https://xxxxxxxxxx.net/nlds_vod/xxxxxxxxxx/vod/2018/02/28/cd264e10-dd54-3de2-df90-4c1cac104dcb/v1/stream/cd264e10-dd54-3de2-df90-4c1cac104dcb_1_4500_pc.mp4.m3u8"""
    

    ffplay 说

    必须指定输入文件

    但输入文件是在 -i 参数

    这个命令有什么问题?


    请注意,该实现是现有批处理脚本的一部分,因此需要使用批处理文件内部的语法。

    1 回复  |  直到 8 年前
        1
  •  2
  •   mklement0    4 年前

    从呼叫时 cmd.exe ,事情变得棘手;下面是一个简化的示例:

    powershell -c "ffmpeg.exe -h "^""User-Agent: usra`r`nCookies: parm=a=1&b=2; session=1"^"""
    

    简而言之: 从…起 命令。exe文件 /批处理文件,以传递PowerShell的CLI应视为双引号参数的内容 整体内部 "..." 字符串传递给 -Command ( -c ) :

    • 在里面 Windows PowerShell ( powershell.exe ):使用 "^""..."^"" (原文如此)

    • 在里面 PowerShell(Core)v6+ ( pwsh.exe ):使用 ""...""

    • 注:

      • 虽然 \"...\" 工作于 二者都 PowerShell版本,不起作用 稳健地 调用时 从…起 命令。exe文件 ,尤其是在本例中,由于要引用的字符串包含 命令。exe文件 元字符 例如 & -见下文。

      • 然而 从…起 无shell上下文 ,例如任务调度器和Windows Run 对话框( WinKey-R ), \"...\" 工作稳健 由于版本不可知,以及与如何 最 CLIs预期 " 查尔斯。被逃脱。

    作为一般要求,文字 % 查尔斯。必须转义为 %% 防止它们被解释为 命令。exe文件 变量引用 在批处理文件中 . 从 命令提示符 , things are unfortunately more complicated .


    • 这个 的命令行 PowerShell 最好作为 仅有一个的 , "..." -封闭字符串 ,通过参数 -c类 (简称 -命令 ,其中 Windows PowerShell 是默认值,但在PowerShell(Core)v6+中已更改,现在默认为 -File ).

    • 自PowerShell CLI以来 条状物 未替换的 " 命令行分析期间的字符 , 之前 将结果解释为PowerShell代码,任何 " 要保留的实例 作为最终执行命令的一部分 必须是 逃脱 (请注意PowerShell- 内部 `" 用于逃避 " ; 或者,在 "..." 仅限字符串, "" 可以使用)。

    • "^""..."^"" ( Windows PowerShell )以及 ""..."" ( PowerShell(Core)v6+ )整体内部 "..." -c类 参数确保 命令。exe文件 自身解释 ... 在双引号字符串中,这就是这些转义形式的原因 强健的 .

    • 如果 \"...\" 在内部使用 "..." 从…起 命令。exe文件 (仅识别 "" 作为逃犯 " ),它实际上会看到 ... 作为存在 外部 双引号字符串,这将导致值包含 命令。exe文件 元字符,例如 & 和 | 到 中断命令 . 比较以下调用 命令。exe文件 :

      # OK - prints verbatim: The king of "Rock  &  Roll"
      C:\>powershell.exe -c " 'The king of "^""Rock  &  Roll"^""' "
      C:\>pwsh.exe -c " 'The king of ""Rock  &  Roll""' "
      
      # !! BROKEN - cmd.exe interprets "&" as a metachar.
      C:\>powershell.exe -c " 'The king of \"Rock  &  Roll\"' "
      C:\>pwsh.exe -c " 'The king of \"Rock & Roll\"' "
      
      • 注:您 能够 修复的使用 \" 通过 ^ -逃离这样的 命令。exe文件 字符(例如。, ^& ),但这需要您仔细分析字符串并确定哪些特定部分 命令。exe文件 视为未引用。如果你犯了错误 ^ 将作为字符串的文字部分保留。的优点 "^"" / "" 方法是,您无需担心此类陷阱。

    作为旁白: 'this '"is "'text' 做 不 以Bash中的方式创建单个字符串;在PowerShell中:

    • 作为一个独立的 表示 ,它会导致 语法错误 (尝试自行执行);你必须使用 ('this ' + "is " + 'text') 或使用 仅有一个的 带引号的字符串( 'this is text' ).

    • 作为 论点 传递给命令(程序),它被解释为 3个不同的 参数-请参阅 this answer 对于这种令人惊讶的行为的解释。