Page 1 of 1

Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 7:42 am
by zeograz
Product, version and build: Powershell Studio 5.6.167.0
32 or 64 bit version of product: 64-Bit
Operating system: Win 10 Ver 1809
64 bit OS: 64-Bit


$buttonRUN_Click={

(Get-Service | where { $_.Status -eq 'Running' }) | sort | Out-GridView -outputmode Single -Title "Restart Service" | Restart-Service
# (Get-Service | where { $_.Status -eq 'Running' }) | sort | Out-GridView -Passthru -Title "Restart Service" | Restart-Service (Produces same error in
Powershell studio when running from form)
}

Produces this error (after selecting a service to restart in ogv window), when run from Powershell Studio Form (but not when running in regular Powershell window on same system):

>> Running (Confirmation.psf) Script...
>> Platform: V5 64Bit (STA) (Forced)
ERROR: Restart-Service : Service 'World Wide Web Publishing Service (W3SVC)' cannot be stopped due to the following error: Cannot open W3SVC service on computer '.'.
Confirmation.psf (867, 4): ERROR: At Line: 867 char: 4
ERROR: + Restart-Service
ERROR: + ~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
ERROR: + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand
ERROR:

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 8:00 am
by mxtrinidad
Ah! I see now. Let's take a look at this issue and get back to you.
Thanks

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 8:20 am
by mxtrinidad
OK! The issue you're experiencing is due to admin rights.

You need to open PowerShell Studio as an Administrator in order for the restart service to work in the form.

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 8:26 am
by Alexander Riedel
This is usually a permission problem and sounds similar to this:
https://serverfault.com/questions/35742 ... powershell
Your 'regular' Powershell console may run in admin mode (it is easy to miss that caption indicator), so check that.
It is also possible that the threading model interferes, but the error message points more towards permissions.

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 8:28 am
by Alexander Riedel
Also, you do not need to run PowerShell Studio as admin, there is a switch on the ribbon to run your scripts elevated.

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 8:37 am
by mxtrinidad
Easy to forget when you work all the time as an admin and already have control of your system.
PowerShellStudio_Elevation_Button_2019-10-07_11-34-46.png
PowerShellStudio_Elevation_Button_2019-10-07_11-34-46.png (31.88 KiB) Viewed 2548 times

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Mon Oct 07, 2019 1:10 pm
by zeograz
Already tried, using that toggle switch (unfortunately get same error).

Also, simply just running the command in console window of Powershell Studio results in same error, just so you're aware.

Execution Policy verified as unrestricted in the Powershell Studio console window. However, strangely even with Elevated toggle on, same error.

Outside of Powershell Studio the error does not occur in Powershell Admin window.

Don't know if it helps, but even trying to set the execution policy (even though already verified it's unrestricted) in the Powershell Studio console window produces this error related to a Powershell registry key (also tried it after toggling the Elevated switch and restarting Powershell Studio):
PowershellStudioError.PNG
PowershellStudioError.PNG (64.31 KiB) Viewed 2506 times
Thanks!
Mark

Re: Psf Out-Gridview -Passthru | Restart-Service produces error

Posted: Wed Oct 09, 2019 5:13 am
by zeograz
Is anyone else able to duplicate/refute the error in this version?
Thanks