8

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.

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()
2
  • 3
    There are no braces ({} after if or else) Commented Jun 16, 2014 at 14:27
  • Nevermind. I already found the bug. Thanks again ! Commented Jun 17, 2014 at 6:32

2 Answers 2

14

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"
    }
...
Sign up to request clarification or add additional context in comments.

4 Comments

Thansk for your reply. But I have now another problem with the script. The loop for each isn't wokring anymore. I only see the last computer in my txt file.
You are doing foreach ($objdisk in $colDisks) but $colDisks will only hold the last computer because this line $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DeviceID ='D:'" keeps reassigning the variable. You should instead make that line add a new computer to an array named $colDisks. This should fix your problem. If it does not, I would recommend asking a new question about it. You will receive the best help that way.
Thanks for your reply. I fixed the problem alread. I writhe the hole script again and the aren't any bugs now.
2

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. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.