简体   繁体   中英

Powershell script to check disk free space

I am running below powershell script to get the disk space information from list of servers but i have been getting below error while running this script,

I am quite new to powershell and did some research and could find the solution.

Error

An Empty pipe element is not allowed.
At line:6 char:81
+ @{Label = 'Free Space (%) ; Expression = {"{0:P0}" -f }} | <<<<
+CategoryInfo : ParserError: <:> [], ParentContainsErrorrecordsException
+ FullyQualifiedErrorId : EmptyPipeElement

$DiskReport | 

Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
          @{Label = "Drive Letter";Expression = {$_.DeviceID}},
          @{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size 
 / 1gb)}},
          @{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( 
 $_.Freespace / 1gb ) }},
          @{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f 
 ($_.freespace/$_.size)}} |

Export-Csv -path "c:\data\server\ServerStorageReport\DiskReport\DiskReport_$logDate.csv" -NoTypeInformation

Here's a quote from the article PowerShell Code Breaks: Break Line, Not Code

When a line of code is too long, it needs to continue on the next line. This is often referred to as line continuation, or sometimes breaking your code (because you break the line into pieces, not because you actually broke the code and it no longer works). However, at times, this is exactly what happens—a piece of perfectly working code becomes nonfunctioning because it was inproperly broken into pieces.

I would certainly recommend reading that article as it covers the problem you are having with line spacing.


So, just tidy up your line spacing and your code might do what you want...

$DiskReport | Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
    @{Label = "Drive Letter";Expression = {$_.DeviceID}},
    @{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
    @{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
    @{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |
    Export-Csv -path "c:\data\server\ServerStorageReport\DiskReport\DiskReport_$logDate.csv" -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM