此脚本列出了所有服务。获得服务后,通过单击开始按钮选择任何要监视的服务,它运行良好,但是表单被卡住或无法控制。当我使用while循环连续运行程序时,我什至无法最小化它。请看看我的代码。您可以在函数Monitor
中找到此循环。
# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()
# Start Creating Functions
function GetProcesses{
# Reset the columns and content of listview_Processes before adding data to it.
$listview_Processes.Items.Clear()
$listview_Processes.Columns.Clear()
# Get a list and create an array of all running processes
$Processes = Get-Service | Select Name,DisplayName
# Compile a list of the properties stored for the first indexed process "0"
$ProcessProperties = $Processes[0].psObject.Properties
# Create a column in the listView for each property
$ProcessProperties | ForEach-Object {
$listview_Processes.Columns.Add("$($_.Name)") | Out-Null
}
# Looping through each object in the array, and add a row for each
foreach ($Process in $Processes) {
# Create a listViewItem, and assign it it's first value
$ProcessListViewItem = New-Object System.Windows.Forms.ListViewItem($Process.Name)
# For each properties, except for 'Id' that we've already used to create the ListViewItem,
# find the column name, and extract the data for that property on the current object/process
$Process.PSObject.Properties | Where {$_.Name -ne "Name"} | ForEach-Object {
$ColumnName = $_.Name
$ProcessListViewItem.SubItems.Add("$($Process.$ColumnName)") | Out-Null
}
# Add the created listViewItem to the ListView control
# (not adding 'Out-Null' at the end of the line will result in numbers outputred to the console)
$listview_Processes.Items.Add($ProcessListViewItem) | Out-Null
}
# Resize all columns of the listView to fit their contents
$listview_Processes.AutoResizeColumns("HeaderSize")
}
function Monitor {
$time= (Get-Date).AddMinutes(10).ToString("d.M.yyyy hh:mm tt")
$Tab = [char]9
# Since we allowed 'MultiSelect = $true' on the listView control,
# Compile a list in an array of selected items
$SelectedProcesses = @($listview_Processes.CheckedIndices)
$IdColumnIndex = ($listview_Processes.Columns | Where {$_.Text -eq "Name"}).Index
while ($true) {
# For each object/item in the array of selected item, find which SubItem/cell of the row...
$SelectedProcesses | ForEach-Object {
$ProcessId = ($listview_Processes.Items[$_].SubItems[$IdColumnIndex]).Text
$service_status=Get-Service $ProcessId
$sts = $service_status.Status
$nam = $service_status.DisplayName
if ($sts -eq "Running") {
#$Textbox_output.col='Green'
$Textbox_output.AppendText("`n")
$Textbox_output.Appendtext("{0}`n" -f $time +"$Tab" +" $nam" + " $sts" )
} else {
$Textbox_output.ForeColor='Red'
$Textbox_output.AppendText("`n")
$Textbox_output.Appendtext("{0}`n" -f $time +"$Tab" +" $nam" + " $sts" )
}
}
Start-Sleep 5
}
GetProcesses
}
# Drawing form and controls
$Form_HelloWorld = New-Object System.Windows.Forms.Form
$Form_HelloWorld.Text = "Monitor"
$Form_HelloWorld.Size = New-Object System.Drawing.Size(1000,528)
$Form_HelloWorld.FormBorderStyle = "FixedDialog"
$Form_HelloWorld.TopMost = $true
$Form_HelloWorld.MaximizeBox = $true
$Form_HelloWorld.MinimizeBox = $true
$Form_HelloWorld.ControlBox = $true
$Form_HelloWorld.StartPosition = "CenterScreen"
$Form_HelloWorld.Font = "Segoe UI"
# Adding a label control for service to Form
$label_HelloWorld = New-Object System.Windows.Forms.Label
$label_HelloWorld.Location = New-Object System.Drawing.Size(8,8)
$label_HelloWorld.Size = New-Object System.Drawing.Size(240,32)
$label_HelloWorld.TextAlign = "MiddleLeft"
$label_HelloWorld.Text = "Service List:"
$Form_HelloWorld.Controls.Add($label_HelloWorld)
# Adding a listView control to Form, which will hold all process information
$Global:listview_Processes = New-Object System.Windows.Forms.ListView
$listview_Processes.Location = New-Object System.Drawing.Size(8,40)
$listview_Processes.Size = New-Object System.Drawing.Size(400,202)
$listview_Processes.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$listview_Processes.View = "Details"
$listview_Processes.CheckBoxes=$true
$listview_Processes.FullRowSelect = $true
$listview_Processes.MultiSelect = $true
$listview_Processes.Sorting = "None"
$listview_Processes.AllowColumnReorder = $true
$listview_Processes.GridLines = $true
$listview_Processes.Add_ColumnClick({SortListView $_.Column})
$Form_HelloWorld.Controls.Add($listview_Processes)
# Adding another button control to Form
$button_EndProcess = New-Object System.Windows.Forms.Button
$button_EndProcess.Location = New-Object System.Drawing.Size(568,450)
$button_EndProcess.Size = New-Object System.Drawing.Size(240,32)
$button_EndProcess.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right
$button_EndProcess.TextAlign = "MiddleCenter"
$button_EndProcess.Text = "Start"
$button_EndProcess.Add_Click({Monitor})
$Form_HelloWorld.Controls.Add($button_EndProcess)
# Adding a label control for OutputTxt to Form
$label_OutputTxt = New-Object System.Windows.Forms.Label
$label_OutputTxt.Location = New-Object System.Drawing.Size(450,8)
$label_OutputTxt.Size = New-Object System.Drawing.Size(240,32)
$label_OutputTxt.TextAlign = "MiddleLeft"
$label_OutputTxt.Text = "Output:"
$Form_HelloWorld.Controls.Add($label_OutputTxt)
# Adding output text control to Form
$Textbox_output = New-Object System.Windows.Forms.TextBox
$Textbox_output.Location = New-Object System.Drawing.Size(450,40)
$Textbox_output.Size = New-Object System.Drawing.Size(530,400)
$Textbox_output.MultiLine = $True
$Textbox_output.ScrollBars = "Vertical"
$Form_HelloWorld.Controls.Add($Textbox_output)
# Show form with all of its controls
$Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate(); GetProcesses})
#[Void] $Form_HelloWorld.ShowDialog()
[System.Windows.Forms.Application]::Run($Form_HelloWorld)