Powershell CSV列

时间:2017-10-12 15:03:08

标签: powershell csv formatting

脚本报告VDHX大小和文件大小,并在输出结尾添加日期。我计划每天运行此操作,以了解用户配置文件磁盘空间的使用量。

$lc   = "\\V22XX003\User Profile Disks\"
$ddr  = Get-ChildItem "$lc*.vhdx"
$time = (Get-Date).ToString("dd-MM-yyyy");
$vhd  = Get-Vhd -Path $ddr | Select Path, Size, Filesize
$vhd | ForEach-Object {
    $_ | Add-Member -MemberType NoteProperty -Name Date -Value "$time"
}
$vhd | Export-Csv -NoTypeInformation -Path \\Besson\c$\udpsz\sizes.csv -Append

这给了我这样的输出:

"Path","Size","FileSize","Date"
"\\V22XX003\User Profile Disks\UVHD-S-1-5-21-242017650-1976910495-1888779859-1168.vhdx","5368709120","2218786816","12-10-2017"
"\\V22XX003\User Profile Disks\UVHD-S-1-5-21-242017650-1976910495-1888779859-1169.vhdx","5368709120","2386558976","12-10-2017"
"\\V22XX003\User Profile Disks\UVHD-S-1-5-21-242017650-1976910495-1888779859-1174.vhdx","5368709120","2285895680","12-10-2017"

我面临的问题是可能会添加或删除VHDX名称,因此无法对其进行硬编码,我希望能够转置数据以使列成为路径。

Desired output

如果我疯狂或狂欢,请告诉我...... :)

***这是我最终编写的代码,它将为每个VDHX写一个文件,并报告每行的大小最大大小和日期,以便导入到excel中。  我希望有一天它会帮助像我这样的noobie :)

$csvout = "C:\testing\out\" #Path to the location you would like to write the report
$time = (Get-Date).ToString("dd-MM-yyyy"); #Change the format of the date here
$vhdstore = "C:\testing\vhdx\" #where the VHDX are stored
$Files = Get-ChildItem -Path "$vhdstore*.vhdx" #path to the VDHX files you want
$Files | ForEach-Object {
$FileFullName = $_.Name #in all honestesty I ripped this line from another script.
$TempFileName = "$csvout$FileFullName.csv"
$vhd = Get-Vhd -Path "$vhdstore$FileFullName" | Select Size,Filesize #get the properties we want in the output
add-member -inputObject $vhd -membertype noteproperty -name Date -value $time #Adding the date to the object
if (-Not (Test-Path $TempFilename)) { #this is just to check if the csv file is already created, if not make new file
$vhd | Export-Csv -Path $TempFileName -NoClobber -NoTypeInformation -Delimiter ","
}

0 个答案:

没有答案