![](/img/trans.png)
[英]Windows10/Powershell: How to use -include parameter when using Get-Childitem?
[英]Powershell Get-childitem script not saves output on Windows 10 machines or when pushed remotely
我寫了一個簡單的腳本,它應該從 C: drive (*.pdf, *.jpg, *.xls, *.doc) 的文件夾中收集所有本地文檔並將它們保存到C:\TEMP
下的“計算機名”文本文件中C:\TEMP
文件夾。
我已經能夠在 Windows 11 機器上本地啟動它而沒有問題! 當我試圖在 Windows 上做同樣的事情時,10 機器文本文件是空的!
我已將其縮小到用戶帳戶交互,就像在 Windows 10 機器上一樣。 如果此腳本是從 PowerShell 以當前用戶身份在 Windows 10 上運行的,則該腳本似乎會生成 output。 如果它是以管理員身份運行(本地或通過遠程以管理員身份推送)我得到空文件。 我將不勝感激任何指導。
代碼:
$FindDate=(Get-Date).adddays(-180)
[System.IO.Directory]::CreateDirectory('C:\TEMP')
{Get-ChildItem -Path C:\ -Directory} |
Where-Object Name -NotIn @('Windows','Program Files','$Recycle.Bin','Windows.old') |
% { Get-ChildItem -Include *.doc,*.docx,*.pdf,*.jpg,*jpeg,*.xls,*.xlsx -File -
Recurse} -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -ge $FindDate } |
Select LastWriteTime,Name,Directory |
Out-File C:\TEMP\$env:Computername.txt
當前代碼存在一些問題:
例如你這樣做:
{Get-ChildItem -Path C:\ -Directory}
這定義了一個表達式並且不會執行:
PS C:\Users\vcxy> {Get-ChildItem -Path C:\ -Directory}
Get-ChildItem -Path C:\ -Directory
PS C:\Users\vcxy>
你可能想要:
PS C:\Users\vcxy> Get-ChildItem -Path C:\ -Directory
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/15/2022 2:46 PM AMD
d----- 6/5/2021 2:10 PM PerfLogs
d----- 4/19/2022 6:56 PM plc_debug
d-r--- 9/22/2022 6:26 PM Program Files
d-r--- 6/12/2022 5:02 PM Program Files (x86)
d----- 9/27/2022 6:43 PM TEMP
d----- 9/27/2022 5:06 PM tmp
d-r--- 10/6/2021 12:44 PM Users
d----- 4/8/2020 2:18 PM VM
d----- 9/22/2022 10:46 PM Windows
我會根據需要更新您的版本:
$FindDate=(Get-Date).adddays(-180)
#This works but you could also do new-item C:\temp -ItemType Directory
[System.IO.Directory]::CreateDirectory('C:\TEMP')
$folders = Get-ChildItem -Path C:\ -Directory | Where-Object {$_.Name -Notmatch 'Windows|Program Files|$Recycle\.Bin|Windows\.old'}
$files = @(
$folders | % {
Get-ChildItem -Path $_.fullname -Include *.doc,*.docx,*.pdf,*.jpg,*jpeg,*.xls,*.xlsx -File -Recurse -ErrorAction:SilentlyContinue | Where-Object {$_.LastWriteTime -ge $FindDate}
}
)
#You should use export-csv, its better for later processing of the data
If ($files){
$files | Select LastWriteTime,Name,Directory | export-csv C:\TEMP\$env:Computername.txt
}
如果找到文件,它只會創建 output 文件,沒有文件沒有 output 文件或關於原始版本,沒有文件 output 文件中沒有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.