简体   繁体   English

Powershell缺失语句块后if

[英]Powershell Missing statement block after if

I have the follow code. 我有以下代码。 But there is a {[( )]} missing somewhere. 但是在某处遗漏了{[()]}。 I can't find it. 我找不到它。 I is in the 'if section. 我在'if部分。 But I dont know why it isnt working If someone can give a tip or something is it will be create. 但我不知道为什么它不起作用如果有人可以给出一个提示或什么是它将是创造。

Thanks for reading. 谢谢阅读。

$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()

In PowerShell, all if-statements need braces to enclose their bodies. 在PowerShell中,所有if语句都需要括号来包含它们的主体。 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语句部分应如下所示:

...
    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. 您只使用一个if语句。 The syntax for if is as follows: if的语法如下:

***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). 只要返回布尔值(true或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'. 您应该在')'之后打开括号并在'else'之前关闭。 Afterwards, remember to put curly brackets in 'else' statement accordingly. 之后,请记住在'else'语句中相应地放置大括号。

EDIT: 编辑:

iCodez has posted already the fixed script. iCodez已发布已修复的脚本。 :-) :-)

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

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