如何在特殊的PowerShell链接命令4命令提示符?
通常,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)
链式PowerShell命令也可以从CMD命令行调用:
C:> powershell notepad; notepad
本文介绍了一种创建.Net 4.0 PowerShell提示的方法,即使.Net 2.0是您OS上的活动框架。 你创建一个.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:WindowsMicrosoft.NETassemblyGAC_MSILSystem.IO.Compression.FileSystemv4.0_4.0.0.0__b77a5c561934e089System.IO.Compression.FileSystem.dll"
即使链接在ps4提示符下,也可以运行:
C:> ps4
PS> Add-Type -Path "C:xSystem.IO.Compression.FileSystem.dll"; `
> $src = "C:xxl"; $zip="C:xxl.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)
问题:在标准命令提示符下启动ps4时,链接上述语句失败(错误已完成在文章底部):
C:> ps4 Add-Type -Path "C:xSystem.IO.Compression.FileSystem.dll"; $src = "C:xxl"; $zip="C:xxl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)
但是,所有上述方法都有效。 为什么? 我该如何做这项工作?
The term 'C:xxl' 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:xSystem.IO.Compression.FileSystem.dll; $src = C:xxl <<<< ; $zip=C:xxl.zip; [io.compression.zipfile]::CreateFromDirectory($src, $zip) + CategoryInfo : ObjectNotFound: (C:xxl:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException The term 'C:xxl.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:xSystem.IO.Compression.FileSystem.dll; $src = C:xxl; $zip=C:xxl.zip <<<< ; [io.compression.zipfile]::CreateFromDirectory($src, $zip) + CategoryInfo : ObjectNotFound: (C:xxl.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:xSystem.IO.Compression.FileSystem.dll; $src = C:xxl; $zip=C:xxl.zip; [io.compression.zipfile]::CreateFromDirectory <<<< ($src, $zip) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
将菊花链命令传递给powershell.exe
时,将删除字符串周围的双引号。 Add-Type
不受此影响,因为路径不包含空格,所以它不需要引号:
Add-Type -Path C:xSystem.IO.Compression.FileSystem.dll # <- this works
但是,在赋值操作中,PowerShell要求字符串用引号括起来,否则它会将该字符串解释为命令并尝试执行它:
$src = C:xxl # <- this would try to run a (non-existent) command 'C:xxl'
# and assign its output to the variable instead of assigning
# the string "C:xxl" to the variable
这就是你观察到的前两个错误的原因。 第三个错误是后续错误,因为两个路径变量未正确初始化。
您应该能够通过转义双引号来避免这种行为:
ps4 Add-Type -Path "C:xSystem.IO.Compression.FileSystem.dll"; $src = "C:xxl"; $zip="C:xxl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)
或者用单引号替换它们(因为后者不被CMD识别为引用字符,因此按原样传递给PowerShell,它将它们识别为引号字符):
ps4 Add-Type -Path 'C:xSystem.IO.Compression.FileSystem.dll'; $src = 'C:xxl'; $zip='C:xxl.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:xxl" -Zipfile "C:xxl.zip"
如果将执行策略更改为RemoteSigned
或Unrestricted
,并且更改.ps1文件的默认处理程序,则甚至可以像这样运行脚本:
zip.ps1 -Path "C:xxl" -Zipfile "C:xxl.zip"
链接地址: http://www.djcxy.com/p/29039.html
上一篇: How to Chain Commands at a Special PowerShell 4 Command Prompt?