[英]Powershell exporting to csv
I have text file which contain some shared paths (eg \\\\INTBMCELB\\Abbott ADD HR Finance
or \\\\INTBMCELB\\Abbott ADD HR Finance_VSS
etc). 我有包含一些共享路径的文本文件(例如\\\\INTBMCELB\\Abbott ADD HR Finance
或\\\\INTBMCELB\\Abbott ADD HR Finance_VSS
等)。
I read all the paths in from this text file and the folders within each path. 我从该文本文件以及每个路径中的文件夹中读取了所有路径。
The details of folders are loaded from a .csv
file. 文件夹的详细信息是从.csv
文件加载的。
Currently I have a field named size
in this .csv
file and I need to add one more field called 'file category'. 目前,我在此.csv
文件中有一个名为size
的字段,我需要再添加一个名为“文件类别”的字段。
The file category
field should show a description based on it's file size: file category
字段应显示基于其文件大小的描述:
Currently this is what I am doing: 目前,这是我在做什么:
$infile = 'C:\Users\417193\Desktop\MyShareBatch\SharePaths.txt'
$outdir = 'C:\Users\417193\Desktop\MyShareBatch\MyShareOuts'
foreach ($dir in (Get-Content $infile)) {
Get-ChildItem -Path $dir -Filter *.* -Recurse | Select-Object Name,
@{Name="Owner";Expression={(Get-ACL $_.fullname).Owner}},CreationTime,
@{Name="FileModifiedDate";Expression={$_.LastWriteTime}},
@{Name="FileAccessedDate";Expression={$_.LastAccessTime}},
@{Name="Attributes";Expression={$_.Attributes}},
@{l='ParentPath';e={Split-Path $_.FullName}},
@{Name="DormantFor(days)";Expression={[int]((Get-Date)-$_.LastWriteTime).TotalDays}},
@{N="FileCategory";E={Get-FileSizeCategory($_)}},
@{Name="Size";Expression={if($_.PSIsContainer -eq $True){(New-Object -com Scripting.FileSystemObject).GetFolder( $_.FullName).Size} else {$_.Length}}}
}
Function Get-FileSizeCategory($Object){
if( $Object.PSIsContainer )
{
$Length = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($Object.FullName).Size
}
else
{
$Length = $_.Length
}
switch( $Length ) {
{$_ -le 10KB} {$Category="Tiny"; break}
{$_ -le 1MB} {$Category="Medium"; break}
{$_ -le 16MB} {$Category="Large"; break}
{$_ -le 128MB} {$Category="Huge"; break}
default {$Category="Gigantic"}
}
}
When I try to execute the above script from a remote server, the file category field is not getting updated. 当我尝试从远程服务器执行上述脚本时,文件类别字段未得到更新。 I've also tried with a local server path, but still the same issue exist. 我也尝试过使用本地服务器路径,但是仍然存在相同的问题。
On my local machine, the script execute successfully with all fields. 在我的本地计算机上,脚本对所有字段都成功执行。 I tried to remove the export to .csv
and print the output but the issue still exist. 我试图删除导出到.csv
并打印输出,但是问题仍然存在。
Try moving the entire _Function Get-FileSizeCategory_
below $outdir
and before foreach
. 尝试将整个_Function Get-FileSizeCategory_
$outdir
以下,并_Function Get-FileSizeCategory_
foreach
之前。
Also _Function Get-FileSizeCategory_
is not returning anything so add return $Category
just above the closing brace Ie: _Function Get-FileSizeCategory_
也不返回任何内容,因此在_Function Get-FileSizeCategory_
大括号上方添加return $Category
:
Function Get-FileSizeCategory($Object){
......
default {$Category="Gigantic"}
}
return $Category
}
That should do it. 那应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.