Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:28:26+00:00 2026-05-22T11:28:26+00:00

In this script below. When I enter the whole script into the powershell command

  • 0

In this script below. When I enter the whole script into the powershell command line then call it with a server name, it works fine. But when i call it from this script:

`sl C:\PowershellScripts
 . ./psscript_Get-FreeSpaceFrag.ps1
 $svl = gc 'C:\PowershellScripts\ebi_serverlist.txt'
 $x = {foreach ($s in $svl) {write-host "Getting Disk Info for Server $s" -   
 foregroundcolor "Green"; Get-FreeSpaceFrag $s; start-sleep -s 5; }}
 $x.invoke() | export-csv "C:\PowershellScripts\DiskInfo.csv" -NoTypeInformation`

It will not work, meaning that the csv file is empty after it processes for a while.

Function Get-FreeSpaceFrag ($s)
{ 
       trap {write-host "Can't connect to WMI on server $s"  -ForeGroundColor "Red" 
         continue 
} 
 $dt = get-date 
 $Scope = new-object System.Management.ManagementScope "\\$s\root\cimv2" 
 $query = new-object System.Management.ObjectQuery "SELECT * FROM Win32_Volume" 
 $searcher = new-object System.Management.ManagementObjectSearcher $scope,$query 
 $SearchOption = $searcher.get_options() 
 $timeout = new-timespan -seconds 10 
 $SearchOption.set_timeout($timeout) 
 $SearchOption 
 $searcher.set_options($SearchOption) 
 $volumes = $searcher.get() 

        $fr = {foreach ($v in $volumes | where {$_.capacity -gt 0}){ 
        $frag=($v.defraganalysis().defraganalysis).totalPercentFragmentation 
        $v | Add-Member -Name Frag -MemberType NoteProperty -Value $frag -Force -        
        PassThru 
        } } 
       $fr.invoke() | select @{N="Server";E={$_.Systemname}}, DriveLetter, Label,                
       Capacity, FreeSpace, @{N="PercentFree";E={"{0,9:N0}" -f  
       (($_.FreeSpace/1gb)/($_.Capacity/1gb)*100)}}, Frag, @{N="InfoDate";E={$dt}}

 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:28:27+00:00Added an answer on May 22, 2026 at 11:28 am

    I think you’re making this a bit harder than it should be i.e. I’m not sure why you need part of the code in an anoymous scriptblock? Try this instead:

     . ./psscript_Get-FreeSpaceFrag.ps1
    Get-Content 'C:\PowershellScripts\ebi_serverlist.txt' | 
        Foreach {Write-Host "Getting Disk Info for Server $_" -foregroundcolor "Green" `
                 Get-FreeSpaceFrag $_} |
        Export-Csv "C:\PowershellScripts\DiskInfo.csv" -NoTypeInformation
    

    Not sure if it will solve the problem but you can also simplify this part of your function:

    $volumes | Where {$_.capacity -gt 0} | Foreach { $_ | Add-Member NoteProperty Frag `
             ($_.defraganalysis().defraganalysis.totalPercentFragmentation) -PassThru} |
      Select @{N="Server";E={$_.Systemname}}, DriveLetter,Label,Capacity,FreeSpace, 
             @{N="PercFree";E={"{0,9:N0}" -f (($_.FreeSpace/1gb)/($_.Capacity/1gb)*100)}},
             Frag, @{N="InfoDate";E={$dt}
    

    BTW rather than invoking a scriptblock like this $fr.invoke() the canonical way in PowerShell is to use the call operator like so &$fr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script: select name,create_date,modify_date from sys.procedures order by modify_date desc I can
I have this script that only lets users enter a single tag but I
First let me explain that I have this script that should let users enter
I have a powershell script (below) which seems to work but prints an error
I've created this script below for a assignment I have. It asks for a
For this script, it checks to see if the file is a microsoft words
I'm using this script to display all the images in a folder, but I
Well basically I have this script that takes a long time to execute and
I have this script which basically toggles a bgColor class on and off so
I got this script called rapidshare link checker v2 php by Bigfish At the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.