简体   繁体   English

Powershell导出到CSV

[英]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字段应显示基于其文件大小的描述:

  • Tiny (0 KB - 10 KB) 小(0 KB-10 KB)
  • Small (10 KB - 100 KB) 小(10 KB-100 KB)
  • Medium (100 KB – 1 MB) 中(100 KB – 1 MB)
  • Large (1 MB - 16MB) 大(1 MB-16MB)
  • Huge (16 MB - 128 MB) 巨大(16 MB-128 MB)
  • Gigantic (> 128 MB) 硕大(> 128 MB)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM