GUI for managing system services (in process)

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

GUI for managing system services (in process)

Post by lontru »

Trying to create a tool for managing services and in the process learn better gui design logic :D

Usage. Type in target(computer) query for services.
Search for a service or click.
Stop or Start the service. (working on the code for this)

When i press the stop button the service get stop but is there a way to refresh the gridview and reselect the service row I just stopped? right now it will select the first row in the gridview.

Did i use the wrong event for this? see error ind scr

Code: Select all

ERROR: Cannot index into a null array.
savefiledialog1.psf (324, 28): ERROR: At Line: 324 char: 28
ERROR: + ... service.Text = $($datagridviewResults.SelectedRows[0].Cells[0].Value)
ERROR: +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : NullArray
Image

form attached.
Attachments
savefiledialog1.psf
(27.64 KiB) Downloaded 100 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI for managing system services (in process)

Post by jvierra »

The service objects are dynamic. They should update the grid if any property changes.
Your "service.Text" should be "$service.Text". It appears you forgot the "$".
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI for managing system services (in process)

Post by lontru »

it does has $

here the code.

Code: Select all

$datagridviewResults_SelectionChanged = {
	if ($datagridviewResults)
	{
		$label1_service.Text = $($datagridviewResults.SelectedRows[0].Cells[0].Value)
	}
	$ServiceStatus = (Get-Service -DisplayName ($($datagridviewResults.SelectedRows[0].Cells[0].Value)) -ComputerName $textbox1.Text).Status
	if ($ServiceStatus -eq 'Running')
	{
		$buttonStop.Enabled = $true
		$buttonStart.Enabled = $false
		$label1_servicestatus.ForeColor = 'Green'
	}
	
	if ($ServiceStatus -eq 'Stopped')
	{
		$buttonStop.Enabled = $false
		$buttonStart.Enabled = $true
		$label1_servicestatus.ForeColor = 'Red'
	}
	
	$label1_servicestatus.Text = $ServiceStatus
	
}
also getting error on this line:

Code: Select all

$ServiceStatus = (Get-Service -DisplayName ($($datagridviewResults.SelectedRows[0].Cells[0].Value)) -ComputerName $textbox1.Text).Status
ERROR: Get-Service : Cannot bind argument to parameter 'DisplayName' because it is null.
savefiledialog1.psf (326, 45): ERROR: At Line: 326 char: 45

error only come at the first query so how do i make it not to happen.

Attached the lastest code:
Attachments
savefiledialog1.psf
(28.12 KiB) Downloaded 90 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI for managing system services (in process)

Post by jvierra »

Here is an example of how to do this:
Attachments
savefiledialog1.psf
(25.5 KiB) Downloaded 101 times
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI for managing system services (in process)

Post by lontru »

Let me see if i get this right

$script:loading = $true
above variable change to $false when the grid is made.

Dont get this if -not ??? if variable $loading is $false it trigger the label1.service.Text right?

if(-not $loading){
$label1_service.Text = $datagridviewResults.SelectedRows[0].DataBoundItem['DisplayName']
}

the "$buttonStart_Click" you only opdate the service status value in the datatable?

Code: Select all

$buttonQuery_Click = {
	if ([string]::IsNullOrEmpty($textbox1.Text)){
		$textbox1.Text = "$env:computername"
	}
    $script:loading = $true
	$Services_coll = Get-Service -ComputerName $textbox1.Text  |
        Select-Object -Property DisplayName, Name, Status, StartType 
	$datagridviewResults.DataSource = ConvertTo-DataTable -InputObject $Services_coll
    ([System.Data.DataTable]$datagridviewResults.DataSource).DefaultView.Sort = 'DisplayName'
    $script:loading = $false
}
$buttonSearch_Click={
	SearchGrid
}

$buttonStart_Click={
	$service = Get-Service $datagridviewResults.SelectedRows[0].DataBoundItem['Name']
    if($service.Status -notmatch 'Running|Starting'){
        $service.Start()
        $service.Refresh()
        $datagridviewResults.SelectedRows[0].DataBoundItem['Status'] = $service.Status
        ([system.Data.DataRowView]$datagridviewResults.SelectedRows[0].DataBoundItem).EndEdit()
    }
}

$buttonStop_Click={
	#TODO: Place custom script here
}

$datagridviewResults_SelectionChanged = {
    if(-not $loading){
	    $label1_service.Text = $datagridviewResults.SelectedRows[0].DataBoundItem['DisplayName']
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI for managing system services (in process)

Post by jvierra »

Yes. That is how it works. How you use it is up to you. You need to understand how the service controller and forms work. This is a fairly big couple of items to understand,

I only showed art of how your issues have to be solved. There is another method that checks to see if any row is selected.

Also you must use a DataTable to allow for dynamic update to the row without having to refresh the whole grid. A DataTable also allows for dynamic sorting on any column and allows for more flexible searching. Sorting and search become one line solutions with a table.

I also discovered that you PSF files are corrupt. They appear to work but are missing important components and will eventually fail after certain edits.

I also started to modify your form to demonstrate how to set up a form that does what you are trying to do and allows for better control of the objects.

Here are some of the examples of ho to manage a form. The form will now no longer work correctly with any new controls due to the damaged PSF file.
Attachments
ServiceManager2.psf
(36.04 KiB) Downloaded 110 times
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: GUI for managing system services (in process)

Post by lontru »

How can you see if a psf is corrupt or damage.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: GUI for managing system services (in process)

Post by jvierra »

It just locks up and makes edits behave erratically. n this case it is subtle. Some properties on newly added controls could not be changed and others didn't set properly. Dumping to a PS1 helps to find some of the issues.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked