首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用get-子项命令的输出

如何使用get-子项命令的输出
EN

Stack Overflow用户
提问于 2014-10-04 08:13:47
回答 1查看 1.2K关注 0票数 0

我想把……

代码语言:javascript
复制
Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 

我试过这个

代码语言:javascript
复制
$command ='Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1';

$file= iex $command

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-04 12:07:50

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

然后将该文件夹对象放入Invoke-Expression中。$file =意味着将Invoke-Expression的结果放入$file中。我还注意到单引号中的命令,这意味着Invoke-Expression将尝试展开它。需要进行更多的测试。我猜你想把这件事改一下。

代码语言:javascript
复制
$folder = Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName

这至少会返回您要查找的完整文件夹路径。你想用它做的就是我迷失了方向。如果您至少有DotNet 4.5,则可以从另一个所以问题压缩如下示例

代码语言:javascript
复制
$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社区扩展,您还可以找到一个名为Write-Zip的cmdlet,您也可以使用它

代码语言:javascript
复制
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中创建压缩/压缩文件夹内置的功能的解决方案

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26190957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档