user3480406
user3480406

Reputation: 11

Powershell exporting to csv

I have text file which contain some shared paths (e.g. \\INTBMCELB\Abbott ADD HR Finance or \\INTBMCELB\Abbott ADD HR Finance_VSS etc).
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.
Currently I have a field named size in this .csv file and I need to add one more field called 'file category'.

The file category field should show a description based on it's file size:

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.

Upvotes: 0

Views: 138

Answers (1)

Raf
Raf

Reputation: 10097

Try moving the entire _Function Get-FileSizeCategory_ below $outdir and before foreach.
Also _Function Get-FileSizeCategory_ is not returning anything so add return $Category just above the closing brace I.e.:

Function Get-FileSizeCategory($Object){
        ......
        default        {$Category="Gigantic"}
    }

    return $Category
}

That should do it.

Upvotes: 1

Related Questions