用于检查磁盘可用空间的Powershell脚本

时间:2018-02-27 15:07:44

标签: powershell

我在powershell脚本下面运行以从服务器列表中获取磁盘空间信息,但是在运行此脚本时我遇到了以下错误,

我对powershell很陌生并做了一些研究,可以找到解决方案。

错误

  

不允许使用空管道元素   在行:6 char:81
  + @ {标签='自由空间(%);表达式= {" {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

1 个答案:

答案 0 :(得分:2)

以下是文章PowerShell Code Breaks: Break Line, Not Code

的引用
  

当一行代码太长时,它需要在下一行继续。这通常被称为行继续,或者有时会破坏您的代码(因为您将行分成几部分,而不是因为您实际上破坏了代码而它不再起作用)。然而,有时候,这正是发生的事情 - 一段完美工作的代码变得无法正常运行,因为它被错误地分解成碎片。

我当然会建议您阅读该文章,因为它涵盖了您使用行距的问题。

所以,只需整理你的行间距,你的代码就可以做你想要的......

$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