[英]How to Chain Commands at a Special PowerShell 4 Command Prompt?
通常,PowerShell命令可以与分号链接在一起。 以下将打开2个记事本:
PS> notepad; notepad
您还可以链接更复杂的语句:
PS> Add-Type -AssemblyName System.IO.Compression; `
> $src = "C:\aFolder"; $zip="C:\my.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)
还可以从CMD命令行调用链接的PowerShell命令:
C:\> powershell notepad; notepad
这篇文章描述了一种创建.Net 4.0 PowerShell提示的方法,即使.Net 2.0是操作系统上的活动框架。 您创建一个.cmd脚本,然后运行它。 现在您处于.Net 4.0环境中。
链接也可以在该4.0提示符下工作:
C:\> ps4.cmd
PS> notepad; notepad
而且也可以在标准CMD提示符下运行:
C:\> ps4 notepad; notepad
这篇文章描述了一种对显式路径名执行Add-Type
的方法(需要从ps4提示符引用4.0程序集):
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提示符处链接时,该方法仍然有效:
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时,链接以上语句失败(帖子底部全错误):
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)
但是,以上所有方法均有效。 为什么? 我该如何进行这项工作?
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
时,将删除字符串周围的双引号。 Add-Type
不受此影响,因为该路径不包含空格,因此它不需要引号:
Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll # <- this works
但是,在赋值操作中,PowerShell要求将字符串用引号引起来,否则它将将该字符串解释为命令并尝试执行:
$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
这就是导致您观察到的前两个错误的原因。 第三个错误是后续错误,因为两个路径变量未正确初始化。
您应该可以通过转义双引号来避免此行为:
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,后者会将其识别为引号字符):
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脚本,而不是解决此问题,以便完全避免此问题:
#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文件的默认处理程序,则您甚至可以运行如下脚本:
zip.ps1 -Path "C:\x\xl" -Zipfile "C:\x\xl.zip"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.