Home > Enterprise >  Which command to use for getting Bytes per cluster and Bytes per File record segment data for multip
Which command to use for getting Bytes per cluster and Bytes per File record segment data for multip

Time:02-04

I would like to generate Bytes per cluster and Bytes per File record segment data for over 50 servers into an excel sheet (for a drive D)

I know the command "Fsutil fsinfo ntfsinfo [drive letter:]" provides this info but only for local system.

i tried writing this but it did not work.

"Enter-PSSession Server1

Fsutil fsinfo ntfsinfo D:

Exit-PSSession"

I then executed each command manually and it was working.

Can anyone please help me create a script to get the above mentioned data at one go for 50 servers.

Thank you

CodePudding user response:

Continuing from my comment, you can use cmdlet Invoke-Comand for that:

# you may already have admin permissions on each of the servers, but if not, get craedentials for someone that has
$adminCreds = Get-Credential -Message 'Please add your admin credentials to get server information'

# your list of server names here
$servers    = 'Server01', 'Server02'  # etc. 

# next use 'Invoke-Command' to have each server run the code
$result = Invoke-Command -ComputerName $servers -Credential $adminCreds -ScriptBlock {
    # have each server run the Fsutil command, and return that as PsCustomObject for convenience
    # instead of an array of lines. 
    # to use ConvertFrom-StringData in PowerShell < 7.x, you need to replace the first colon with a equals sign
    # PowerShell versions above 5.1 can use parameter  -Delimiter '='
    [PsCustomObject]((Fsutil fsinfo ntfsinfo D:) -replace '(?<!:.*):', '=' -join "`r`n" | ConvertFrom-StringData)
}

Now you can save the entire result to CSV or limit to the properties you need like

$result | Select-Object PSComputerName, 'Bytes Per Cluster', 'Bytes Per FileRecord Segment' | Export-Csv -Path 'X:\serverInfo.csv' -NoTypeInformation

If you are not sure all of the servers can be reached, do a loop:

$result = foreach ($server in $servers) {
    # test if the server can be reached
    if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
        Invoke-Command -ComputerName $server -Credential $adminCreds -ScriptBlock {
            # have each server run the Fsutil command, and return that as PsCustomObject for convenience
            # instead of an array of lines. 
            # to use ConvertFrom-StringData in PowerShell < 7.x, you need to replace the first colon with a equals sign
            # PowerShell versions above 5.1 can use parameter  -Delimiter '='
            [PsCustomObject]((Fsutil fsinfo ntfsinfo D:) -replace '(?<!:.*):', '=' -join "`r`n" | ConvertFrom-StringData)
        }
    }
    else {
        Write-Warning "Server $server is off-line!"
    }
}

Regex details:

(?<!        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   :        Match the character “:” literally
   .        Match any single character
      *     Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)          
:           Match the character “:” literally
  •  Tags:  
  • Related