Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

powershell - Get LastAccessTime for each subfolders in a csv folder list

I have a csv ($HomeDir) file like this:

Users,Comments,HomeDir
user1,account1,c:folder1
user2,account2,c:folder2
user3,account3,c:folder3

I get succesfully LastAccessTime for each subfolder in 'HomeDir' column with this code:


    $csv = Import-Csv $HomeDir
    foreach ($folder in $csv.HomeDir)  {
                                        Get-ChildItem $folder -ErrorAction SilentlyContinue |
                                            ? {$_.PSIsContainer -eq "True"} | 
                                             Select-Object FullName,  @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}} |
                                             Sort-Object -Descending -Property LastAccessTime |
                                             Export-Csv $newcsv -NoTypeInformation -Append
                                              }

The result of $newcsv is:

"FullName","LastAccessTime"
"c:folder1Sub1","20201223"      
"c:folder1Sub1a","20201223"      
"c:folder1Sub1b","20201223"      
"c:folder2Sub2","20201218"
"c:folder2Sub2a","20201218"
"c:folder3Sub3","20201212"
"c:folder3Sub3a","20201215"
"c:folder3Sub3b","20181215"
"c:folder3Sub3c","20201011"

The questions is: is there a way to assign the related User based on corresponding 'Users' column? It would also be enough for me to get an output like this

"Users","FullName","LastAccessTime"
"user1","c:felder1Sub1","20201223"
"user1","c:folder1Sub1a","20201223"
"user1","c:folder1Sub1b","20201223"
"user2","c:folder2Sub2","20201218"
"user2","c:folder2Sub2a","20201218"
"user3","c:folder3Sub3","20201212"
"user3","c:folder3Sub3a","20201215"
"user3","c:folder3Sub3b","20181215"
"user3","c:folder3Sub3c","20201011"

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of pulling the HomeDir property out before the loop, just reference it inside the loop and add the Users property to your Fullname and LastAccessTime properties.

$properties = @{Name='User'; Expression={$user}},
              'FullName',
              @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}}

Import-Csv $HomeDir | ForEach-Object {
    $user = $_.users
    Get-ChildItem $_.HomeDir -ErrorAction SilentlyContinue | 
        Where-Object {$_.PSIsContainer -eq "True"} | 
            Select-Object $properties |
                Sort-Object -Descending -Property LastAccessTime
} | Export-Csv $newcsv -NoTypeInformation

To make it easier to read I put the properties in a variable beforehand. Also, if you are on powershell version 3 or higher you can use -Directory parameter in lieu of $_.PSIsContainer -eq $true. Finally, if you use the Foreach-Object instead of a foreach loop you can pipe to export and not have to use -Append opening and closing the file several times.

$properties = @{Name='User'; Expression={$user}},
              'FullName',
              @{Name='LastAccessTime'; Expression={$_.LastAccessTime.ToString('yyyyMMdd')}}

Import-Csv $HomeDir | ForEach-Object {
    $user = $_.users
    Get-ChildItem $_.HomeDir -Directory -ErrorAction SilentlyContinue |  
        Select-Object $properties | Sort-Object -Descending -Property LastAccessTime
} | Export-Csv $newcsv -NoTypeInformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...