0

我想拉开拉链

Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 

我试过这个

$command ='Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1';

$file= iex $command

并将 $file 放入 zip 函数中但导致错误。

Get-ChildItem : Access to the path 'C:\Windows\System32\Com\dmp' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Com\dmp:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\config' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\config:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\Configuration' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Configuration:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\ias' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\ias:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\inetsrv\config' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\inetsrv\config:String) [Get-ChildItem], UnauthorizedAccessExcep 
   tion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Fax\Incoming' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\Fax\Incoming:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Fax\Outgoing' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\Fax\Outgoing:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Firewall' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\Firewall:String) [Get-ChildItem], UnauthorizedAccessEx 
   ception
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\HTTPERR' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\HTTPERR:String) [Get-ChildItem], UnauthorizedAccessExc 
   eption
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\WMI:String) [Get-ChildItem], UnauthorizedAccessExcepti 
   on
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\MsDtc' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\MsDtc:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\networklist' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\networklist:String) [Get-ChildItem], UnauthorizedAccessExceptio 
   n
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\spool\PRINTERS' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\spool\PRINTERS:String) [Get-ChildItem], UnauthorizedAccessExcep 
   tion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\spool\SERVERS' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\spool\SERVERS:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\sru' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\sru:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\Tasks' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Tasks:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wbem\MOF' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wbem\MOF:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wdi' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wdi:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wfp' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wfp:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
4

1 回答 1

1

您的代码中缺少某些内容。就目前而言,这不包含有关压缩目录的信息。您显示的代码问题的根源是您要求Invoke-Expression运行文件夹名称。您的 $command 正在返回一个包含一个文件夹的对象。那是你想要的吗?

然后,您将该文件夹对象放入Invoke-Expression. $file =表示您的意思是将结果Invoke-Expression放入$file. 我还注意到你有单引号中的命令,这意味着Invoke-Expression会尝试扩展它。需要进行更多测试。我猜你想稍微改变一下。

$folder = Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName

这至少会返回您正在寻找的完整文件夹路径。你试图用它做的是我迷失的东西。如果你至少有 DotNet 4.5,你可以像这个例子一样从另一个SO 问题中压缩

$folder = Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$destination = "e:\test\myZip.zip"
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder, $destination)

借助PowerShell 社区扩展,您还可以找到一个名为 cmdlet Write-Zip,您也可以使用它

Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 | Write-Zip -IncludeEmptyDirectories -OutputPath E:\Test\MyZip.zip

如果您愿意:这是一个仅使用 PowerShell 内置功能的解决方案使用 Powershell 或命令行在 Windows 中创建压缩/压缩文件夹

于 2014-10-04T12:07:50.367 回答