简体   繁体   English

Powershell脚本检查磁盘可用空间

[英]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, 我正在powershell脚本下运行以从服务器列表中获取磁盘空间信息,但是在运行该脚本时却一直出现以下错误,

I am quite new to powershell and did some research and could find the solution. 我对Powershell相当陌生,并做了一些研究,可以找到解决方案。

Error 错误

An Empty pipe element is not allowed. 不允许使用空管道元素。
At line:6 char:81 在线:6字符:81
+ @{Label = 'Free Space (%) ; + @ {Label ='可用空间(%); Expression = {"{0:P0}" -f }} | 表达式= {“ {0:P0}” -f}} | <<<< <<<<
+CategoryInfo : ParserError: <:> [], ParentContainsErrorrecordsException + CategoryInfo:ParserError:<:> [],ParentContainsErrorrecordsException
+ FullyQualifiedErrorId : EmptyPipeElement + 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 这是来自PowerShell代码中断的文章:换行符,不是代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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