使用PowerShell 2.0提取ZIP

时间:2016-05-21 22:44:57

标签: shell powershell batch-file

我正在编写一个需要提取ZIP文件的脚本。问题是所有部署计算机都是Windows 2008 R2并安装了PowerShell 2.0。我在另一个网站上发现了以下代码,但是我需要从命令行执行所有这些操作,而不是创建PowerShell脚本。

如何编写以下代码,我可以使用powershell.exe -command (New-Object System.Net.ShellApplication)来调用它?

$shell = New-Object -ComObject Shell.Application
$zip = $shell.NameSpace("C:\howtogeeksite.zip")
foreach($item in $zip.Items()) {
    $shell.Namespace("C:\temp\howtogeek").CopyHere($item)
}

2 个答案:

答案 0 :(得分:2)

我在Reddit上找到了它,所以我想我会分享答案......

powershell.exe (new-object -com shell.application).NameSpace("E:\foldername").CopyHere((new-object -com shell.application).NameSpace("E:\test.zip").Items())

答案 1 :(得分:0)

您可以通过加入lookup行来直接将其写入-Command,例如:

;

或者您可以使用base64编码的字符串。

powershell -NoProfile -Command $shell = new-object -com shell.application; $zip = $shell.NameSpace(“C:\howtogeeksite.zip”); foreach($item in $zip.items()) { $shell.Namespace(“C:\temp\howtogeek”).copyhere($item) }

Ex generate命令:

powershell -?

..
# To use the -EncodedCommand parameter:
    $command = 'dir "c:\program files" '
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    powershell.exe -encodedCommand $encodedCommand

使用编码命令:

$command = @'
$shell = New-Object -ComObject Shell.Application
$zip = $shell.NameSpace("C:\howtogeeksite.zip")
foreach($item in $zip.Items()) {
    $shell.Namespace("C:\temp\howtogeek").CopyHere($item)
}
'@

[convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($command))

#output
JABzAGgAZQBsAGwAIAA9ACAAbgBlAHcALQBvAGIAagBlAGMAdAAgAC0AYwBvAG0AIABzAGgAZQBsAGwALgBhAHAAcABsAGkAYwBhAHQAaQBvAG4ADQAKACQAegBpAHAAIAA9ACAAJABzAGgAZQBsAGwALgBOAGEAbQBlAFMAcABhAGMAZQAoABwgQwA6AFwAaABvAHcAdABvAGcAZQBlAGsAcwBpAHQAZQAuAHoAaQBwAB0gKQANAAoAZgBvAHIAZQBhAGMAaAAoACQAaQB0AGUAbQAgAGkAbgAgACQAegBpAHAALgBpAHQAZQBtAHMAKAApACkADQAKAHsADQAKACQAcwBoAGUAbABsAC4ATgBhAG0AZQBzAHAAYQBjAGUAKAAcIEMAOgBcAHQAZQBtAHAAXABoAG8AdwB0AG8AZwBlAGUAawAdICkALgBjAG8AcAB5AGgAZQByAGUAKAAkAGkAdABlAG0AKQANAAoAfQA=