2014-10-04 2 views
0

Я хотел заархивировать из положитьКак использовать вывод Get-ChildItem команды

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 

и положить $ файла в функции почтового индекса, но привело к ошибке ,

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 
+0

Я не понимаю, почему он запускает папку system32, когда я упоминал путь e: \ test. – user3606823

ответ

1

В коде отсутствует что-то отсутствующее. Как бы то ни было, это не содержит информации об zipping каталоге. Корень проблемы с кодом, который вы показываете, заключается в том, что вы запрашиваете Invoke-Expression для запуска имени папки. Ваша команда $ возвращает объект, содержащий одну папку. Это то, что вы хотели?

Затем вы берете этот объект папки и помещаете его в 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 question

$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 Community Extensions вы также найдете командлет 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. Creating a zipped/compressed folder in Windows using Powershell or the command line

+0

Большое спасибо Мэтт. Это то, что я искал. Еще раз спасибо. – user3606823

 Смежные вопросы

  • Нет связанных вопросов^_^