Powershell Missing statement block after if

大城市里の小女人 提交于 2019-12-06 00:08:21

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 >

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"
    }
...

You use only one if statement. The syntax for if is as follows:

***IF (condition returns true) { action }***

Basically the condition is stated in round brackets, while the execution block is in curly brackets.

A simple example:

if (1 -eq 1) { Write-host "1 is equal to 1" }

Of course in the example above the condition is always true. You can use any condition you want as long as it returns boolean value (true or false).

Inside your script, in the

if (Test-Connection -ComputerName $strComputer -Count 1 -Quiet) 

there is missing curly bracket. You should open bracket after ' ) ' and close before 'else'. Afterwards, remember to put curly brackets in 'else' statement accordingly.

EDIT:

iCodez has posted already the fixed script. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!