Вопрос

I have two sheets on my excel spread sheet. The script is placing the info in the proper sheets but it is putting blanks in the sheet. If Computer1 is placed in the first sheet and the Computer2 is in the second sheet. The first cell in the second sheet will be blank and the Computer2 entry will be in the second second row instead of the first. How can I stop this from happening? It is creating a ghost cell for the one that is in the first sheet. Here is my code:

$erroractionpreference = "SilentlyContinue"
$date = 0
$date = get-date -format "yyyy-MMM-dd-hhmm"
$date

$Excel = New-Object -Com Excel.Application

$Excel.visible = $True

# Create 3 worksheets

$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Add()

# Assign each worksheet to a variable

$Sheet1 = $Excel.Worksheets.Item(1)
$Sheet2 = $Excel.WorkSheets.Item(2)

# name the worksheet.

$Sheet1.Name = "Machines with PerfStat"
$Sheet2.Name = "No PerfStat"

#Create Heading for General Sheet

$Sheet1.Cells.Item(1, 1) = "Machine_Name"

$Sheet1.Cells.Item(1, 2) = "PerfStat Installed"

$Sheet2.Cells.Item(1, 1) = "Machine_Name"

$Sheet2.Cells.Item(1, 2) = "PerfStat_Installed"

#Create Heading for No PerfStat

$colSheets = ($Sheet1, $Sheet2)

#Auto Fit all sheets in the Workbook

foreach ($colorItem in $colSheets)
{

$WorkBook = $colorItem.UsedRange

$WorkBook.EntireColumn.AutoFit()

clear

}

foreach ($colorItem in $colSheets)
{

$intRow = 2

$WorkBook = $colorItem.UsedRange

$WorkBook.Interior.ColorIndex = 20

$WorkBook.Font.ColorIndex = 11

$WorkBook.Font.Bold = $True

}


$Computers = Get-Content -Path C:\temp\servers.txt


foreach ($Server in $computers)
{
remove-variable reply
$reply = Invoke-WebRequest http://$Server":1055" | Select-Object Content


if ($reply.Content -like "*Windows PerfStat v1.1.4*")
{

    $Sheet1.Cells.Item($intRow, 1) = $server

    $Sheet1.Cells.Item($intRow, 2) = "Yes"


}
else
{

    $Sheet2.Cells.Item($intRow, 1) = $server

    $Sheet2.Cells.Item($intRow, 2) = "No"


}

$intRow = $intRow + 1

}

$outputfile = "c:\temp\" + $date.toString() + "-PerfStat"
$Excel.SaveAs($outputfile)
$Excel.Close()
Это было полезно?

Решение

I have figured it out! I placed this code at the bottom of the script and it sorts both sheets before creating the file and saving it.

$Range1 = $Sheet1.range('A2:D2500')
$Range2 = $sheet1.Range('B2')
$Range1.Sort($Range2, 1)
$Range.entirecolumn.autofit()

$Range1 = $Sheet2.range('A2:D2500')
$Range2 = $sheet2.Range('B2')
$Range1.Sort($Range2, 1)
$Range.entirecolumn.autofit()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top