问题描述
团队 -- 尝试扩展我的 powershell 脚本,该脚本扫描共享驱动器以包括捕获文件老化信息(请参阅下面的代码)。我尝试使用 TimeSpan 函数,但它返回空白值。
我尝试简化脚本并使用 Get-Date -- 但它也没有返回任何值,而所有其他数据元素都包含信息。
我也尝试创建一个变量 $DT = Get-Date 并包含它......但它也返回空白。
任何帮助将不胜感激。
$target_files = Get-ChildItem \\ [source path] \* -recurse -include "*"
$target_files | select-object -Property @{n='Length in MB';e={[math]::Round(($_.Length / 1MB),2)}},DirectoryName,name,CreationTime,LastWriteTime,New-TimeSpan Get-Date - LastWriteTime |
Export-csv \\ [destination path] \\directoryfiles_Inventory.csv -NoTypeinformation
Import-Csv \\ [destination path] \\directoryfiles_Inventory.csv
解决方法
我猜您正在寻找 now
和 lastWriteTime
日期之间的时差。如果是这种情况,您可以执行以下操作:
$properties = @(
@{
Name = 'Length in MB'
Expression = {[math]::Round(($_.Length / 1MB),2)}
}
'DirectoryName'
'Name'
'CreationTime'
'LastWriteTime'
@{
Name = 'Time Difference'
Expression = {
([datetime]::Now - $_.LastWriteTime).TotalHours.ToString('#.# Hours')
}
}
)
Get-ChildItem . | Select-Object $properties | Export-Csv "C:\destination\path\export.csv" -NoTypeInformation
每个项目看起来像这样:
Length in MB : 0.04
DirectoryName : C:\Users\example.user
Name : test.txt
CreationTime : 4/27/2021 10:12:57 AM
LastWriteTime : 4/27/2021 10:12:57 AM
Time Difference : 391.8 Hours