Powershell 缺少 if 后的语句块 [英] Powershell Missing statement block after if
本文介绍了Powershell 缺少 if 后的语句块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码.但是在某处缺少一个 {[( )]} .我找不到.我在如果"部分.但我不知道为什么它不起作用如果有人可以提供小费或其他东西,它将被创建.
I have the follow code. But there is a {[( )]} missing somewhere. I can't find it. I is in the 'if section. But I dont know why it isnt working If someone can give a tip or something is it will be create.
感谢阅读.
$date = (get-date).AddDays(-1).ToString("yyyMMdd")
$erroractionpreference = "SilentlyContinue"
$Excel = New-Object -comobject Excel.Application
$Excel.visible = $True
$ExcelSheet = $Excel.Workbooks.Add()
$ExcelCell = $ExcelSheet.Worksheets.Item(1)
$ExcelCell.Cells.Item(1,1) = "Machine Name"
$ExcelCell.Cells.Item(1,2) = "Online"
$ExcelCell.Cells.Item(1,3) = "Drive"
$ExcelCell.Cells.Item(1,4) = "Total size (GB)"
$ExcelCell.Cells.Item(1,5) = "Free Space (GB)"
$ExcelCell.Cells.Item(1,6) = "Free Space (%)"
$ExcelCell.cells.item(1,7) = "Used Space (GB)"
$ExcelMakeup = $ExcelCell.UsedRange
$ExcelMakeup.Interior.ColorIndex = 19
$ExcelMakeup.Font.ColorIndex = 11
$ExcelMakeup.Font.Bold = $True
$ExcelMakeup.EntireColumn.AutoFit()
$intRow = 2
$colComputers = get-content "E:\servers.txt"
foreach ($strComputer in $colComputers)
{
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DeviceID ='D:'"
}
foreach ($objdisk in $colDisks)
{
if (Test-Connection -ComputerName $strComputer -Count 1 -Quiet)
$ExcelCell.Cells.Item($introw, 2) = "Online"
$ExcelCell.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$ExcelCell.Cells.Item($intRow, 3) = $objDisk.DeviceID
$ExcelCell.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.Size/1GB)
$ExcelCell.Cells.Item($intRow, 5) = "{0:N0}" -f ($objDisk.FreeSpace/1GB)
$ExcelCell.Cells.Item($intRow, 6) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
$ExcelCell.cells.item($introw, 7) = "{0:N0}" -f ([double]$objDisk.Size/1GB - [double]$objDisk.Freespace/1GB)
else
$ExcelCell.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$ExcelCell.Cells.Item($intRow, 2) = "Offline"
$ExcelCell.Cells.Item($intRow, 3) = "x"
$ExcelCell.Cells.Item($intRow, 4) = "x"
$ExcelCell.Cells.Item($intRow, 5) = "x"
$ExcelCell.Cells.Item($intRow, 6) = "x"
$ExcelCell.cells.item($introw, 7) = "x"
$intRow = $intRow + 1
}
$ExcelMakeup.EntireColumn.AutoFit()
$Excel.ActiveWorkbook.SaveAs("E:\results\" + $date + "_" + (get-date -format "HHmm") + "_D_Drive.xlsx")
$Excel.Workbooks.Close()
$Excel.Quit()
推荐答案
在 PowerShell 中,所有 if-statements 都需要大括号来包围它们的主体.下面是一个演示:
In PowerShell, all if-statements need braces to enclose their bodies. Below is a demonstration:
PS > if ($true) write 'true'
At line:1 char:10
+ if ($true) write 'true'
+ ~
Missing statement block after if ( condition ).
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingStatementBlock
PS > if ($true) { write 'true' }
true
PS >
因此,您脚本的 if 语句部分应如下所示:
Therefore, the if-statement section of your script should look like this:
...
if (Test-Connection -ComputerName $strComputer -Count 1 -Quiet)
{
$ExcelCell.Cells.Item($introw, 2) = "Online"
$ExcelCell.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$ExcelCell.Cells.Item($intRow, 3) = $objDisk.DeviceID
$ExcelCell.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.Size/1GB)
$ExcelCell.Cells.Item($intRow, 5) = "{0:N0}" -f ($objDisk.FreeSpace/1GB)
$ExcelCell.Cells.Item($intRow, 6) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size)
$ExcelCell.cells.item($introw, 7) = "{0:N0}" -f ([double]$objDisk.Size/1GB - [double]$objDisk.Freespace/1GB)
}
else
{
$ExcelCell.Cells.Item($intRow, 1) = $strComputer.ToUpper()
$ExcelCell.Cells.Item($intRow, 2) = "Offline"
$ExcelCell.Cells.Item($intRow, 3) = "x"
$ExcelCell.Cells.Item($intRow, 4) = "x"
$ExcelCell.Cells.Item($intRow, 5) = "x"
$ExcelCell.Cells.Item($intRow, 6) = "x"
$ExcelCell.cells.item($introw, 7) = "x"
}
...
这篇关于Powershell 缺少 if 后的语句块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文