如何在特殊的PowerShell 4命令提示符处链接命令? [英] How to Chain Commands at a Special PowerShell 4 Command Prompt?

查看:95
本文介绍了如何在特殊的PowerShell 4命令提示符处链接命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,PowerShell命令可以与分号链接在一起。以下将打开2个记事本:

Normally, PowerShell commands can be chained with semicolons. The following opens 2 notepads:

PS> notepad; notepad

您还可以链接更复杂的语句:

You can also chain a more complex statement:

PS> Add-Type -AssemblyName System.IO.Compression; `
> $src = "C:\aFolder"; $zip="C:\my.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)

链式PowerShell命令可以也可以从CMD命令行调用:

Chained PowerShell commands can also be called from a CMD command-line:

C:\> powershell notepad; notepad

这篇文章描述了一种创建.Net 4.0 PowerShell提示符的方法,即使.Net 2.0是操作系统上的活动框架也是如此。您创建一个.cmd脚本,然后运行它。现在您处于.Net 4.0环境中。

This post describes a method to create a .Net 4.0 PowerShell prompt, even if .Net 2.0 is the active framework on your OS. You create a .cmd script, and run that. Now you're in a .Net 4.0 environment.

链接也可以在该4.0提示符下运行:

Chaining also works at that 4.0 prompt:

C:\> ps4.cmd
PS> notepad; notepad

在标准CMD提示符下也可以使用:

And also works from standard CMD prompt:

C:\> ps4 notepad; notepad

这篇文章描述了对显式路径名执行 Add-Type 的方法(需要从ps4提示符中引用4.0程序集):

This post describes a way to do Add-Type to an explicit pathname (needed to reference 4.0 assemblies from the ps4 prompt):

Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll"

,即使在ps4提示符下链接时:

That works, even when chained at the ps4 prompt:

C:\> ps4
PS> Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; `
> $src = "C:\x\xl"; $zip="C:\x\xl.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)

问题:当在标准命令提示符下启动ps4时,链接以上语句失败(帖子底部完全错误):

Problem: chaining the above statement fails when ps4 is launched at a standard command prompt (error in full at bottom of post):

C:\> ps4 Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; $src = "C:\x\xl"; $zip="C:\x\xl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)

但是,所有以上方法有效。为什么?我该如何工作?

Yet, all the above methods work. Why? How can I make this work?

The term 'C:\x\xl' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:73
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl <<<< ; $zip=C:\x\xl.zip;
[io.compression.zipfile]::CreateFromDirectory($src, $zip)
    + CategoryInfo          : ObjectNotFound: (C:\x\xl:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'C:\x\xl.zip' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:91
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip <<<< ;
[io.compression.zipfile]::CreateFromDirectory($src, $zip)
    + CategoryInfo          : ObjectNotFound: (C:\x\xl.zip:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Exception calling "CreateFromDirectory" with "2" argument(s): "The path is not
of a legal form."
At line:1 char:138
+ Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip;
[io.compression.zipfile]::CreateFromDirectory <<<< ($src, $zip)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException


推荐答案

将菊花链式命令传递给 powershell.exe 时,将删除字符串周围的双引号。 添加类型不受此影响,因为该路径不包含空格,因此它不需要引号:

The double quotes around the strings are removed when the daisy-chained commands are passed to powershell.exe. Add-Type isn't affected by this, since the path doesn't contain spaces, so it doesn't require quotes:

Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll   # <- this works

但是,在赋值操作中,PowerShell要求将字符串用引号引起来,否则它将字符串解释为命令并尝试执行: / p>

However, in an assignment operation PowerShell requires strings to be in quotes, otherwise it will interpret the string as a command and try to execute it:

$src = C:\x\xl  # <- this would try to run a (non-existent) command 'C:\x\xl'
                #    and assign its output to the variable instead of assigning
                #    the string "C:\x\xl" to the variable

这就是导致您观察到的前两个错误的原因。第三个错误是随后的错误,因为两个路径变量没有正确初始化。

That is what causes the first two errors you observed. The third error is a subsequent fault, because the two path variables weren't properly initialized.

您应该可以通过转义双引号来避免此行为:

You should be able to avoid this behavior by escaping the double quotes:

ps4 Add-Type -Path \"C:\x\System.IO.Compression.FileSystem.dll\"; $src = \"C:\x\xl\"; $zip=\"C:\x\xl.zip\"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)

或通过替换它们用单引号引起来(因为后者不能被CMD识别为引号字符,因此按原样传递给PowerShell,它确实将它们识别为引号字符):

or by replacing them with single quotes (because the latter are not recognized by CMD as quoting characters and are thus passed as-is to PowerShell, which does recognize them as quoting characters):

ps4 Add-Type -Path 'C:\x\System.IO.Compression.FileSystem.dll'; $src = 'C:\x\xl'; $zip='C:\x\xl.zip'; [io.compression.zipfile]::CreateFromDirectory($src, $zip)






但是,我建议不要创建并运行适当的PowerShell脚本,而不是解决此问题,这样您就可以完全避免此问题:


However, rather than working around this issue I'd recommend creating and running proper PowerShell scripts, so you can avoid this problem entirely:

#requires -version 4
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$true)]
  [string]$Path,
  [Parameter(Mandatory=$true)]
  [string]$Zipfile,
)

Add-Type -Assembly 'System.IO.Compression.FileSystem' | Out-Null
[IO.Compression.ZipFile]::CreateFromDirectory($Path, $Zipfile)

脚本将像这样运行:

powershell.exe -File zip.ps1 -Path "C:\x\xl" -Zipfile "C:\x\xl.zip"

如果将执行策略更改为 RemoteSigned Unrestricted ,还更改.ps1文件的默认处理程序,甚至可以运行脚本像这样:

If you change the execution policy to RemoteSigned or Unrestricted and also change the default handler for .ps1 files you could even run the script like this:

zip.ps1 -Path "C:\x\xl" -Zipfile "C:\x\xl.zip"

这篇关于如何在特殊的PowerShell 4命令提示符处链接命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆