question

MrEdge-3552 avatar image
0 Votes"
MrEdge-3552 asked DSPatrick edited

Get IIS websites size

Im trying to get a list of sites from IIS (8.5) to include the folder size but cant get this to work.

Below is the current code i have that is working without the size

 Import-Module webAdministration # Required for Powershell v2 or lower  
    
 $filepath = C:\sites.csv
 $sites = get-website
    
 foreach($site in $sites) {
     $name = $site.name
     $bindings = $site.bindings.collection.bindinginformation.split(":")
     $ip = $bindings[0]
     $port = $bindings[1]
     $hostHeader = $bindings[2]
     "$name,$hostHeader,$ip,$port" | out-host
     "$name,$hostHeader,$ip,$port" | out-file $filePath -Append
 }

I then attempted to add in this line

 $size = Get-ChildItem -Directory -Force|ForEach {"{0,-30} {1,-30} {2:N2}MB" -f $_.Name, $_.LastWriteTime, ((Get-ChildItem $_ -Recurse|Measure-Object -Property Length -Sum -ErrorAction Stop).Sum/1MB)}

but that didnt work either.

I then attempted with

 $size = Get-ChildItem $name + "\folderName\" | Measure-Object -Property Length -sum

which was getting closer but i think my syntax is wrong with $name + "\folderName\" as im getting a series of errors. I say this is close as it has the path to the directory but it doesnt exist. The directory would exist if i can add the foldername to the $name variable?

Where am i going wrong? Or how could i retrieve the parent of each website folder size?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

This adds the count, and size in bytes to the output.

  $filepath = "C:\sites.csv"
  $sites = get-website
        
  foreach($site in $sites) {
      $name = $site.name
      $bindings = $site.bindings.collection.bindinginformation.split(":")
      $ip = $bindings[0]
      $port = $bindings[1]
      $hostHeader = $bindings[2]
      $size = Get-ChildItem ([system.Environment]::ExpandEnvironmentVariables($site.physicalPath)) -Recurse | Measure-Object -Property Length -sum
      "$name,$hostHeader,$ip,$port,$($size.count),$($size.sum)" | out-host
      "$name,$hostHeader,$ip,$port,$($size.count),$($size.sum)" | out-file $filePath -Append
  }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.