通常,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
This post描述了一种创建.Net 4.0 PowerShell提示的方法,即使.Net 2.0是您操作系统上的活动框架。您创建一个.cmd脚本,然后运行它。现在您处于.Net 4.0环境中。
Chaining也适用于4.0提示符:
C:\> ps4.cmd
PS> notepad; notepad
并且还可以从标准CMD提示符开始:
C:\> ps4 notepad; notepad
This post描述了一种对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
答案 0 :(得分:1)
将菊花链命令传递给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"