I have a script that worked a few months ago but no longer works. it collects values from SQL queries and adds them to word tables. after the script runs I check the variables and they have the data in them so its not the variables. this is how im adding to word table
$selection.Style="Heading 1"
$Selection.TypeText("Tools Reports")
$Selection.TypeParagraph()
$selection.Style="Heading 2"
$Selection.TypeText("HPSim Data")
$Selection.TypeParagraph()
$selection.Style="Normal"
$Selection.TypeText("The table below shows the status of HPSim Agents at Braintree AD")
$Selection.TypeParagraph()
$selection.Style="Normal"
$objRange = $Selection.Range
$UserTable =$Word.ActiveDocument.Tables.Add($Word.Selection.Range, 1, 4)
$UserTable = $objDoc.Tables.Item(1)
$UserTable.Cell(1,1).Range.Text = "Measured Item"
$UserTable.Cell(1,2).Range.Text = $A1
$UserTable.Cell(1,3).Range.Text = $A2
$UserTable.Cell(1,4).Range.Text = $A3
$UserTable.Rows.Add()
$UserTable.Cell(2,1).Range.Text = "Servers in Active Directory"
$UserTable.Cell(2,2).Range.Text = $B1
$UserTable.Cell(2,3).Range.Text = $B2
$UserTable.Cell(2,4).Range.Text = $B3
$UserTable.Rows.Add()
$UserTable.Cell(3,1).Range.Text = "Servers Audited"
$UserTable.Cell(3,2).Range.Text = $C1
$UserTable.Cell(3,3).Range.Text = $C2
$UserTable.Cell(3,4).Range.Text = $C3
$UserTable.Rows.Add()
$UserTable.Cell(4,1).Range.Text = "HPSim Running"
$UserTable.Cell(4,2).Range.Text = $D1
$UserTable.Cell(4,3).Range.Text = $D2
$UserTable.Cell(4,4).Range.Text = $D3
$UserTable.Rows.Add()
$UserTable.Cell(5,1).Range.Text = "% of AD Servers with HPSim Running"
$UserTable.Cell(5,2).Range.Text = $E1
$UserTable.Cell(5,3).Range.Text = $E2
$UserTable.Cell(5,4).Range.Text = $E3
$UserTable.Rows.Add()
$UserTable.Cell(6,1).Range.Text = "% of Audited Servers with HPSim Running"
$UserTable.Cell(6,2).Range.Text = $F1
$UserTable.Cell(6,3).Range.Text = $F2
$UserTable.Cell(6,4).Range.Text = $F3
$Selection.EndKey($END_OF_STORY)
$selection.MoveDown()
$UserTable.AutoFormat(23)
$UserTable.Columns.AutoFit()
$Selection.TypeParagraph()
the first field of the first row populates but SECOND FIELDS are blank and I get error
I derive the values for $A AND $A1 USING
$A = Invoke-SqlQuery -Query “SELECT COUNT(Name) FROM adcompsbraintree” -Server “CLUSQL423IIT\IITDB” -Database ‘ExcelData’
$A=$A.‘Column#0’
$APERCENT = $A/$A
$A1= “{0:P}” -f $APercent
the error im getting for each is
Specified cast is not valid.
At C:\DataReporting\Resources\EssexWeeklyReport.ps1:2418 char:1
- $UserTable.Cell(2,3).Range.Text = $A1
-
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException