MainForm.psf (76): ERROR: At Line: 76 char: 35
ERROR: + if (![string]::IsNullOrWhiteSpace <<<< ($InitialDirectory)) { $openFileDialog.InitialDirectory = $InitialDirectory }
ERROR: + CategoryInfo : InvalidOperation: (IsNullOrWhiteSpace:String) [], RuntimeException
ERROR: + FullyQualifiedErrorId : MethodNotFound
ERROR:
You selected the file: C:\temp\Cameras.csv
ERROR: += : The '+=' operator failed: Item has already been added. Key in dictionary: 'ResponseLength' Key being added: 'ResponseLength'.
ERROR:
MainForm.psf (116): ERROR: At Line: 116 char: 13
ERROR: + $Result += <<<< [Management.Automation.PSObject] @{
ERROR: + CategoryInfo : InvalidOperation: (System.Collections.Hashtable:PSObject) [], RuntimeException
ERROR: + FullyQualifiedErrorId : OperatorFailed
ERROR:
ERROR: += : The '+=' operator failed: Item has already been added. Key in dictionary: 'ResponseLength' Key being added: 'ResponseLength'.
ERROR:
MainForm.psf (116): ERROR: At Line: 116 char: 13
ERROR: + $Result += <<<< [Management.Automation.PSObject] @{
ERROR: + CategoryInfo : InvalidOperation: (System.Collections.Hashtable:PSObject) [], RuntimeException
ERROR: + FullyQualifiedErrorId : OperatorFailed
ERROR:
ERROR: += : The '+=' operator failed: Item has already been added. Key in dictionary: 'ResponseLength' Key being added: 'ResponseLength'.
ERROR:
MainForm.psf (116): ERROR: At Line: 116 char: 13
ERROR: + $Result += <<<< [Management.Automation.PSObject] @{
ERROR: + CategoryInfo : InvalidOperation: (System.Collections.Hashtable:PSObject) [], RuntimeException
ERROR: + FullyQualifiedErrorId : OperatorFailed
ERROR:
Not sure why I'm getting this error.
Below is the button action.
Code: Select all
$buttonCheckCameras_Click= {
#TODO: Place custom script here
$data = Import-Csv $CSVFileName.Text
$buttonCheckCameras.Text = "Checking Camera's, Please Wait"
Foreach ($item in $data)
{
$name = $item.name
$address = $item.ip
$username = $item.username
$password = $item.password
$time = try
{
$Request = $null
$result1 = Measure-Command { $Request = Invoke-WebRequest -Uri $address }
$result1.TotalMilliseconds
}
catch
{
$Request = $_.Exception.Response
$time = -1
}
$Result += [Management.Automation.PSObject] @{
Time = Get-Date
Uri = $address
Name = $name
StatusCode = [int] $Request.StatusCode;
StatusDescription = $Request.StatusDescription;
ResponseLength = $Request.RawContentLength;
TimeTaken = $time;
}
}
#Fillin Datagrid
$row = @("$($Entry.name)", "$($Entry.uri)", "$($Entry.StatusCode)", "$($Entry.StatusDescription)", "$($Entry.timetaken)")
$datagridview.Rows.Add($row)
}
Code: Select all
function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect)
{
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
if (![string]::IsNullOrWhiteSpace($InitialDirectory)) { $openFileDialog.InitialDirectory = $InitialDirectory }
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }
$openFileDialog.ShowHelp = $true # Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.
$openFileDialog.ShowDialog() > $null
if ($AllowMultiSelect) { return $openFileDialog.Filenames } else { return $openFileDialog.Filename }
}
$filePath = Read-OpenFileDialog -WindowTitle "Select Camera CSV File" -InitialDirectory 'C:\' -Filter "CSV files (*.csv)|*.csv"
if (![string]::IsNullOrEmpty($filePath)) { Write-Host "You selected the file: $filePath" }
else { "You did not select a file." }
$csv = Import-Csv $filePath
$Result = @()
Foreach($item in $csv)
{
$name = $item.name
$address = $item.ip
$username = $item.username
$password = $item.password
$time = try
{
$Request = $null
$result1 = Measure-Command {$Request = Invoke-WebRequest -Uri $address }
$result1.TotalMilliseconds
}
catch
{
$Request = $_.Exception.Response
$time = -1
}
$Result += [Management.Automation.PSObject] @{
Time = Get-Date
Uri = $address
Name = $name
StatusCode = [int] $Request.StatusCode;
StatusDescription = $Request.StatusDescription;
ResponseLength = $Request.RawContentLength;
TimeTaken = $time;
}
}
#Prepare Outut in HTML
if ($Result -ne $null)
{
$Outputreport = "<HTML>
<TITLE>Camera Availability Report</TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Taile"">
<H2> Camera Availability Report </H2></font>
<Table border=1 cellpadding=5 cellspacing=5>
<TR bgcolor=gray align=center>
<TD><B>Camera ID</B></TD>
<TD><B>IP Address</B></TD>
<TD><B>Status Code</B></TD>
<TD><B>Status Description</B></TD>
<TD><B>Time Taken(ms)</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.name)</TD>
<TD>$($Entry.uri)</TD>
<TD align=center>$($Entry.StatusCode)</TD>
<TD align=center>$($Entry.StatusDescription)</TD>
<TD align=left>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$OutputReport | Out-File C:\temp\CameraAvailability.htm
Invoke-Expression C:\Temp\CameraAvailability.htm