Saving correct data to CSV file

Welcome Forums General PowerShell Q&A Saving correct data to CSV file

This topic contains 1 reply, has 2 voices, and was last updated by

2 years ago.

  • Author
  • #63781

    Points: 11
    Rank: Member

    Need some help on this. I am running the script which which will caluclate the folder size, db size of sites on few server (locally & on NAS path).

    When I run this, some of the foldersize says '0'.

    But when I run individually on one server, it works fine. I guess looping is the problem. Can someone help me get where I am doing wrong.

    Error says: path cannot be found, but folders are present in that location.

    foreach ($server in $abcservers){
    $servername = $server.column1
    $customers += @(Get-ChildItem -Path \\$servername\c$\inetpub\wwwroot\abc | select -expand Name)
    $customers = $customers -replace "`n|`r"
    foreach ($client in $customers){
    Write-Host "Working on $servername : $client..."
    $folders = (Get-ChildItem -Path \\$servername\c$\inetpub\wwwroot\abc\$client\$client -Recurse -ErrorAction Silentlycontinue | Measure-Object -property length -sum)

    If (Test-Path \\abcprdfs\abc\prd\CustomerFiles\$client) {
    $sanfolders = Get-ChildItem -Path \\abcprdfs\abc\prd\CustomerFiles\$client -Recurse -ErrorAction Silentlycontinue | Measure-Object -property length -sum
    $foldersize = "{0:N2}" -f (($folders.sum + $sanfolders.sum) / 1MB)
    else {
    $foldersize = "{0:N2}" -f ($folders.sum / 1MB)

    $db = Get-ChildItem -Path \\abcprddb01\c$\Progra~1\Micros~1\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\$client*.mdf | Measure-Object -property length -sum
    $dbsize = "{0:N2}" -f ($db.sum / 1MB)

    $header = new-object psobject
    $header | Add-Member -membertype noteproperty -name "AppServer" -Value $servername.ToUpper()
    $header | Add-Member -membertype noteproperty -name "CustomerName" -Value $client.ToUpper()
    $header | Add-Member -membertype noteproperty -name "FolderSize(MB)" -Value $foldersize
    $header | Add-Member -membertype noteproperty -name "DBSize(MB)" -Value $dbsize
    $finalout += $header
    Write-Host "$servername : $client complete!"

    $finalout | export-csv C:\temp\usage.csv -Encoding utf8 -NoTypeInformation

  • #63785

    Points: 660
    Helping Hand
    Rank: Major Contributor

    Don posted a nice function to get the size of a folder time ago. You might adapt this to your needs.
    function Get-FolderSize

The topic ‘Saving correct data to CSV file’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort