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.
Locked
User avatar
lontru
Posts: 48
Joined: Fri Aug 18, 2017 4:36 am

GUI for managing system services (in process)

Post by lontru » Fri Nov 23, 2018 2:07 pm

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 10 times

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GUI for managing system services (in process)

Post by jvierra » Fri Nov 23, 2018 2:20 pm

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: 48
Joined: Fri Aug 18, 2017 4:36 am

Re: GUI for managing system services (in process)

Post by lontru » Fri Nov 23, 2018 2:34 pm

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 8 times

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GUI for managing system services (in process)

Post by jvierra » Fri Nov 23, 2018 3:22 pm

Here is an example of how to do this:
Attachments
savefiledialog1.psf
(25.5 KiB) Downloaded 10 times

User avatar
lontru
Posts: 48
Joined: Fri Aug 18, 2017 4:36 am

Re: GUI for managing system services (in process)

Post by lontru » Sun Nov 25, 2018 12:54 pm

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']
    }
}

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GUI for managing system services (in process)

Post by jvierra » Sun Nov 25, 2018 1:22 pm

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 11 times

User avatar
lontru
Posts: 48
Joined: Fri Aug 18, 2017 4:36 am

Re: GUI for managing system services (in process)

Post by lontru » Sun Nov 25, 2018 1:40 pm

How can you see if a psf is corrupt or damage.

User avatar
jvierra
Posts: 13128
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: GUI for managing system services (in process)

Post by jvierra » Sun Nov 25, 2018 2:30 pm

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.

Locked