VMware Cloud Community
DashrathReddy
Contributor
Contributor

PowerCLi Script to fetch file details like FullName, LastWriteTime, Length stored on the datastores

With the help of below command, i can able to fetch the details of files with FullName, LastWriteTime, Length only from one datastore, I am looking for the modified script to get this details from all the datastores present in the VC.

dir vmstore:\Datacenter\localssd | Get-ChildItem | select-object FullName, LastWriteTime, Length

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership

Try like this, but be warned that the VimDatastore provider is not very fast.

Get-ChildItem -Path vmstore: |

Select -first 1 |

ForEach-Object -Process {

   Get-ChildItem -Path "vmstore:/$($_.Name)" -Recurse |

   where{-not $_.PSIsContainer} |

  select FullName,LastWriteTime,Length

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

I got into the below error.. Basically I am looking for the data which can tell me how long the files are not been accessed or used in datastores. (storage cleanup activity), please suggest if you have other-ways to accomplish.

Error received:

Get-ChildItem : An error occurred while communicating with the remote host.

At line:3 char:4

+    Get-ChildItem -Path "vmstore:/$($_.Name)" -Recurse |

+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], HostCommunication

    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.HostCommunication,Microsoft.PowerShell.Commands.GetChildItemCommand

Reply
0 Kudos
LucD
Leadership
Leadership

Could it be that you have no permission to access specific parts of your environment?
What PowerCLI version are you using?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

Hi... I am using "administrator@vsphere.local" credentials and seems I have all the required credentials. Please find the PSVersion details below.

Name                           Value

----                           -----

PSVersion                      5.1.17134.407

PSEdition                      Desktop

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

BuildVersion                   10.0.17134.407

CLRVersion                     4.0.30319.42000

WSManStackVersion              3.0

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

Reply
0 Kudos
LucD
Leadership
Leadership

I mean the PowerCLI version, not the PowerShell version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

Please find the PowerCli version below..

PowerCLI Version

----------------

   VMware PowerCLI 11.0.0 build 10380590

Reply
0 Kudos
LucD
Leadership
Leadership

Can you check with the following?
Does it also produce that error?

Get-ChildItem -Path vmstore: |

   ForEach-Object -Process {

   Get-ChildItem -Path "vmstore:/$($_.Name)"

}

If that works, can you try this one.

Get-ChildItem -Path vmstore: |

   ForEach-Object -Process {

   Get-ChildItem -Path "vmstore:/$($_.Name)" |

   ForEach-Object -Process {

   Get-ChildItem -Path $_.DatastoreBrowserPath | select FullName

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

Hi.. It is working but I could not get LastWriteTime & Length information in it. Attached the screen shot for reference. please assist.

Reply
0 Kudos
LucD
Leadership
Leadership

Can you try the 1st script I gave you again?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

Hi.. I received the same error by using the first script.

Seems I do not have access on some files inside the folders (as we are using "Recurse" parameter and it tries to get information on file level) it is trying to get those details and failing. I tried to get LastWriteTime & Length at folder level and got the attached results (lengh information is missing for folders and files like .iso/.txt turned with the size details). Attached the screen shot for reference.

Get-ChildItem -Path vmstore: |

   ForEach-Object -Process {

   Get-ChildItem -Path "vmstore:/$($_.Name)" |

   ForEach-Object -Process {

   Get-ChildItem -Path $_.DatastoreBrowserPath | select FullName,LastWriteTime,Length

   }

}

Reply
0 Kudos
LucD
Leadership
Leadership

That's what I already suspected earlier.
The problem is that you only get the 1st level this way.

With -Recurse you will stumble on the error.


Give it a try like this

Get-ChildItem -Path vmstore: |

   ForEach-Object -Process {

   Get-ChildItem -Path "vmstore:/$($_.Name)" -Recurse -ErrorAction SilentlyContinue |

  Select FullName, LastWriteTime, Length

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DashrathReddy
Contributor
Contributor

No Luck... I did a try, but same error...

PS C:\> Get-ChildItem -Path vmstore: |

>>    ForEach-Object -Process {

>>

>>    Get-ChildItem -Path "vmstore:/$($_.Name)" -Recurse -ErrorAction SilentlyContinue |

>>

>>   Select FullName, LastWriteTime, Length

>>

>> }

Get-ChildItem : An error occurred while communicating with the remote host.

At line:4 char:4

+    Get-ChildItem -Path "vmstore:/$($_.Name)" -Recurse -ErrorAction Si ...

+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], HostCommunication

    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.HostCommunication,Microsoft.PowerShell.Commands.GetChildItemCommand

Reply
0 Kudos
LucD
Leadership
Leadership

Then I'm afraid I have no other solution at hand.
You will have to solve the connection issue before you can get a full file list.

As an alternative, you only get the list for a specific datastore or specific datastores.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos