• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1590
  • Last Modified:

Powershell Network Drive Used and Free Space

Hi

We would like to find out how much data is on dfs shares and the free space available using a Mapped drive letter or UNC, once we have the information we will create a HTML page with all shares and below information to end users.

I:\share\fileshare1
I:\share\fileshare2
I:\share\fileshare3

\\domain\group\topshare\fileshare1
\\domain\group\topshare\fileshare2
\\domain\group\topshare\fileshare3

the output would be to a csv file and would show the following

Name: Fileshare1 Used: 256GB Free: 78GB

One issue we have is that we are not admin on the server.
0
Si Walton
Asked:
Si Walton
  • 10
  • 9
1 Solution
 
RobSampsonCommented:
Hi, based on the example here:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/08/use-powershell-to-create-a-report-displaying-free-disk-space.aspx

This code would show you the free space for a logical drive F.
Get-WMIObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DeviceID='F:'" | Select Name, ProviderName, @{n='FreeSpace';e={"{0:N2}" -f ($_.freespace/1GB)}}

Open in new window


Rob.
0
 
Si WaltonAuthor Commented:
Hi

Thank you for your solution

Unfortuenetly it will only work for the top level IE 'F:'

We need to be able to drill further down that level to subfolders F:\share\subfolder

would an UNC path be a better solution?

Also i need thefolowing information


 Name: Fileshare1 Used: 256GB Free: 78GB

Thank you

Si
0
 
RobSampsonCommented:
Do you have permission to use the ActiveDirectory module? If you can list DFS Share paths from AD, as shown here:
http://blogs.metcorpconsulting.com/tech/?p=1637

We should be able to get the free space of that share.

Rob.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Si WaltonAuthor Commented:
Yes I can list the DFS share name

\\clu82fs01.compname.uk\filedataf02$\dm_1
0
 
RobSampsonCommented:
Hi, I have spent some time with my DFS and come up with the following code.

All you should need to change is the name of $DomainController to one of the DCs that hosts your DFS.

Regards,

Rob.

$CurrentFolder = Split-Path -Parent $MyInvocation.MyCommand.Definition
$OutputFile = "$CurrentFolder\DFS_Share_Usage.csv"
$DomainController = "YOURDC"

# Go through letters D to Z to find next available drive letter for temporary mapping
For ($i=68; $i -le 90; $i++) {
    $DriveLetter = "$([char]$i)"
    If ((Test-Path "$DriveLetter`:") -eq $False) {Break}
}
Get-WMIObject -ComputerName $DomainContoller -Query "SELECT * FROM Win32_DFSTarget WHERE ServerName <> 'null'" | Select LinkName,ServerName,ShareName | ForEach {
    If (([regex]::Matches($_.LinkName, "\\" )).Count -gt 3) {
        $LinkName = $_.LinkName
        $ServerName = $_.ServerName
        $ShareName = $_.ShareName
        $ServerShare = "\\$($_.ServerName)\$($_.ShareName)"
        New-PSDrive –Name $DriveLetter –PSProvider FileSystem –Root $ServerShare -Persist | 
            Select @{n='LinkName';e={$LinkName}}, @{n='ServerName';e={$ServerName}}, @{n='ShareName';e={$ShareName}}, @{n='Used (GB)';e={"{0:N2}" -f ($_.Used/1GB)}}, @{n='Free (GB)';e={"{0:N2}" -f ($_.Free/1GB)}}, @{n='Free (%)';e={"{0:N2}" -f (($_.Free/1GB)/(($_.Used/1GB)+($_.Free/1GB))*100)}}
        Remove-PSDrive $DriveLetter
    }
} | Export-CSV $OutputFile -NoTypeInformation
Invoke-Item $OutputFile

Open in new window

0
 
Si WaltonAuthor Commented:
sorry where am I going wrong - I added $DomainController = "\\clu82fs01.comname.uk\filedataf02$\dm_1"

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\MyScripts\test.ps1:10 char:28
+ Get-WMIObject -ComputerName <<<<  $DomainContoller -Query "SELECT * FROM Win32_DFSTarget WHERE ServerName <> 'null'" | Select LinkName,ServerName,ShareName | ForEach {
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
 
Invoke-Item : Cannot find path 'C:\MyScripts\DFS_Share_Usage.csv' because it does not exist.
At C:\MyScripts\test.ps1:21 char:12
+ Invoke-Item <<<<  $OutputFile
    + CategoryInfo          : ObjectNotFound: (C:\MyScripts\DFS_Share_Usage.csv:String) [Invoke-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeItemCommand
0
 
RobSampsonCommented:
For $DomainController, just enter the name of one of the domain controllers that is a DFS host. The script will then use WMI to read the Win32_DFSTarget class and list all of the shares that are connected to DFS.

Rob.
0
 
Si WaltonAuthor Commented:
That won't work as I only have access to certain shares.

I need to just read the indervidual DFS path IE \\clu82fs01.compname.uk\filedataf02$\dm_1

Maybe I am asking for something that can't be done with out the admin rights.

The only success I have had is via DOS commands but the formatting isn't great.

Thanks a lot for your help so far Rob, it's very much appreciated

Si
0
 
RobSampsonCommented:
If you don't have acces to the shares, you won't be able to see how much free space there is. At any rate, show me an example of success you have had with a DOS command. We can probably format the output better for you.

The code I wrote won't go to waste anyway because I'm going to use it myself ;-)

Rob.
0
 
Si WaltonAuthor Commented:
Hi Tom

I do have access to all 20 share but there other shares on the Volumes that I don't have access to.

I use a program called T4eDirSize that I batch the 20 shares that we need the info on.

its a messy way of doing it and due to the size of the folders we would prefer GB and we end up having to clean up lines and convert data sizes.

But the good thing is that it does read the unc/dfs path

It gives the following output -

 Tools4ever Directory Size Utility. Version 2.45
Copyright (c) 2003 Tools4ever bv. All rights reserved.
www.tools4ever.com

Total file size (bytes): 206962635714 bytes
Total file size (KB): 202111948 KB
Total file size (MB): 197374 MB
Total number of files: 15598
Total number of directories: 227

Total disk size (bytes): 2198886907904 bytes
Total disk size (MB): 2097021 MB
Total disk free size (bytes): 385061093376 bytes
Total disk free size (MB): 367222 MB

Archive file/directory count: 14951
Compressed file/directory count: 0
Encrypted file/directory count: 0
Hidden file/directory count: 0
Offline file/directory count: 0
Read-only file/directory count: 0
System file/directory count: 22
Temporary file/directory count: 0

Search time: 2253 ms
Error count: 0
0
 
RobSampsonCommented:
OK, well assuming you don't have any rights to list connected targets via WMI, and you do know the share names you need, try this script, and list all of your shares in the $arrShares variable.

Regards,

Rob.

$CurrentFolder = Split-Path -Parent $MyInvocation.MyCommand.Definition
$OutputFile = "$CurrentFolder\Share_Free_Space_Usage.csv"

# Go through letters D to Z to find next available drive letter for temporary mapping
For ($i=68; $i -le 90; $i++) {
    $DriveLetter = "$([char]$i)"
    If ((Test-Path "$DriveLetter`:") -eq $False) {Break}
}
$arrShares = @(
    "\\fileserver1\share1"
    "\\fileserver2\share2"
    )
$arrShares | ForEach {
    $ServerShare = "$_"
    New-PSDrive –Name $DriveLetter –PSProvider FileSystem –Root $_ -Persist | 
        Select @{n='ShareName';e={$ServerShare}}, @{n='Used (GB)';e={"{0:N2}" -f ($_.Used/1GB)}}, @{n='Free (GB)';e={"{0:N2}" -f ($_.Free/1GB)}}, @{n='Free (%)';e={"{0:N2}" -f (($_.Free/1GB)/(($_.Used/1GB)+($_.Free/1GB))*100)}}
    Remove-PSDrive $DriveLetter
} | Export-CSV $OutputFile -NoTypeInformation
Invoke-Item $OutputFile

Open in new window

0
 
Si WaltonAuthor Commented:
Hi Rob

Your a star - I changed the code to just map a Drive letter as I was having issues with "The local device name is already in use."

Also upgraded Powershell to V3 as -Persist was giving me errors due to V2 not using Persist

Thank you for working with me on this, Just made my life a whole lot easier.

Si

$CurrentFolder = Split-Path -Parent $MyInvocation.MyCommand.Definition
$OutputFile = "$CurrentFolder\Share_Free_Space_Usage.csv"
$DriveLetter = "G"

$arrShares = @(
    "\\fileserver1\share1"
    "\\fileserver2\share2"
    )
$arrShares | ForEach {
    $ServerShare = "$_"
    New-PSDrive –Name $DriveLetter –PSProvider FileSystem –Root $_ -Persist | 
        Select @{n='ShareName';e={$ServerShare}}, @{n='Used (GB)';e={"{0:N2}" -f ($_.Used/1GB)}}, @{n='Free (GB)';e={"{0:N2}" -f ($_.Free/1GB)}}, @{n='Free (%)';e={"{0:N2}" -f (($_.Free/1GB)/(($_.Used/1GB)+($_.Free/1GB))*100)}}
    Remove-PSDrive $DriveLetter
} | Export-CSV $OutputFile -NoTypeInformation
Invoke-Item $OutputFile

Open in new window

0
 
Si WaltonAuthor Commented:
Hi Rob

Just checked 3 shares on the same volume and they are all showing as the same Used Space.

Used (GB)      Free (GB)      Free (%)
1,689.26         358.62              17.51
1,689.26              358.62              17.51
1,689.26              358.62              17.51

What can we do to get the Actual Share size?

Si
0
 
RobSampsonCommented:
Hey Si,

Glad to hear it works for you. Very odd about the device name in use issues, maybe Test-Path was not working correctly. I am using PS3.0 as well.

Thanks for the grade.

Rob.
0
 
RobSampsonCommented:
Oh, in relation to your latest question, your are after usage per share, even on the same volume? Free space in that case is not useful because free space is calculated per drive.

I can probably do a tree size type usage for used space though.  I can look into that tomorrow.

Rob.
0
 
Si WaltonAuthor Commented:
Thank you
0
 
RobSampsonCommented:
Hi Si,

I've added a Tree size for the shares listed.

Regards,

Rob.

$CurrentFolder = Split-Path -Parent $MyInvocation.MyCommand.Definition
$OutputFile = "$CurrentFolder\Share_Free_Space_Usage.csv"
$DriveLetter = "G"

$arrShares = @(
    "\\fileserver1\share1"
    "\\fileserver2\share2"
    )
$arrShares | ForEach {
    $ServerShare = "$_"
    $FolderTreeSize = Get-ChildItem $ServerShare -Recurse -Force | Measure-Object -Property length -Sum
    New-PSDrive –Name $DriveLetter –PSProvider FileSystem –Root $_ -Persist | 
        Select @{n='ShareName';e={$ServerShare}}, @{n='Used (GB)';e={"{0:N2}" -f ($_.Used/1GB)}}, @{n='Free (GB)';e={"{0:N2}" -f ($_.Free/1GB)}}, @{n='Free (%)';e={"{0:N2}" -f (($_.Free/1GB)/(($_.Used/1GB)+($_.Free/1GB))*100)}}, @{n='Tree Size (GB)';e={"{0:N2}" -f ($FolderTreeSize.Sum/1GB)}}
    Remove-PSDrive $DriveLetter
} | Export-CSV $OutputFile -NoTypeInformation
Invoke-Item $OutputFile

Open in new window

0
 
Si WaltonAuthor Commented:
Hi Rob

Go to the top of the class - Fantastic job, I take my hat off to you sir

Thank you for taking the time to work with me on this

Best Regrdas

Si
0
 
RobSampsonCommented:
Thanks Si. It's great to be recognised for the work that we Experts do.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now