[英]Powershell export to CSV
我正在嘗試創建一天的文件報告,我目前有一個包含文件名和日期的腳本:
get-childitem -Path \\UNC_PATH_TO_FILE
| where-object {$_.lastwritetime -gt (get-date).addDays(-1)}
| Foreach-Object { $_.Name, $_.CreationTime }
以前我將其導出到每個UNC路徑的文件中,但是現在已經增長並且將導致讀取22個單獨的文件。
我想將其合並為一個CSV文件,該文件可以包含服務器 , 文件名 ( $_.Name
)和日期 ( $_.CreationTime
_。CreationTime)的coll。
這超出了我的Powershell領域,找不到任何真正幫助我的東西。 有人能提供一些幫助嗎?
你可以試試這個:
$paths = "\\server1\unc\path", "\\server1\unc\path"
Get-ChildItem -Path $paths
| Where-Object {$_.lastwritetime -gt (Get-Date).addDays(-1)}
| Select-Object @{n="Server";e={([uri]$_.FullName).Host}},
@{n="Filename";e={$_.Name}},
@{n="Date";e={$_.CreationTime}},
@{n="FileSize(MB)";e={[Math]::Round($_.Length/1MB,3)}}
| Export-Csv test.csv -NoTypeInformation
它從UNC路徑中提取servername,並使用您指定的名稱重命名其他屬性。
這是一個為每個文件創建新對象的示例:
Get-ChildItem -Path \\UNC_PATH_TO_FILE |
Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} |
ForEach-Object {
New-Object PSObject -Property @{
Server = $_.FullName -replace '^\\\\([\w]+)\\.+$','$1'
Name = $_.Name
CreationTime = $_.CreationTime
}
} | Export-Csv -Path files.csv -NoTypeInformation
以下應該有效:
get-childitem -Path \\UNC_PATH_TO_FILE
| where { $_.lastwritetime -gt (get-date).AddDays(-1)}
| foreach-object { $env.COMPUTERNAME,$_.Name, $_.CreationTime}
| Export-Csv -Path foo.txt -NoTypeInformation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.