Score:1

Retrieve parent folder size for each IIS site

cn flag

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?

JosefZ avatar
th flag
Maybe `Get-ChildItem $site.physicalPath`?
Score:0
cn flag

In my testing, the PhysicalPath property can contain cmd.exe formatted variable %SystemDrive%.

When this occurs, you can take the physical path and extract the environment variable name and the remaining path using regex. Pull out the value of the variable and build the path.

$filepath = 'C:\sites.csv'
$sites = get-website

foreach($site in $sites) {
    if($site.physicalPath -match '^%(.+)%(.+)$'){
        $sitedir = Join-Path (Get-Content "env:$($matches.1)") $matches.2
        Get-ChildItem -LiteralPath $sitedir
    }
}

If you confirm this does accurately list the files for each site, it seems you've figured out the measurement portion.

A couple of other suggestions. First, I recommend Export-Csv instead of Out-File.

Next, instead of opening the file and appending over and over (slow) you can collect all the results and then write them.

$results = foreach($site in $sitelist){
    ... code ...
}

$results | Out-File $filepath

Or you can use ForEach-Object and take advantage of the pipeline.

$sitelist | ForEach-Object {
    ... code ...
} | Out-File $filepath

A side benefit of ForEach-Object is the -OutVariable parameter which lets you capture the output in a variable (that is of type ArrayList) and see the output (or pipe it on to more commands.)

Considering my recommendations, I'd give this a shot.

$filepath = 'C:\sites.csv'
$sites = get-website

$sites | ForEach-Object {

    $ip, $port, $hostHeader = $_.bindings.collection.bindinginformation.split(":")

    $path = if($_.physicalPath -match '^%(.+)%(.+)$'){
        Join-Path (Get-Content "env:$($matches.1)") $matches.2
    }
    else{
        $_.physicalpath
    }

    $size = (Get-ChildItem $path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB

    [PSCustomObject]@{
        Name       = $_.name
        FullPath   = $path
        IP         = $ip
        Port       = $port
        HostHeader = $hostHeader
        SizeMB     = $size
   }
} -OutVariable results

$results | Export-Csv $filepath -NoTypeInformation
cn flag
Thanks but the file size using the above was not coming through. It retrieved no value?
Doug Maurer avatar
cn flag
I updated my post
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.