Microsoft Powershell Sith Lord
, ,

Use Powershell for Windows Service Administration

Microsoft instructions for installing Powershell on Linux (Debian).

# Save the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --yes --dearmor --output /usr/share/keyrings/microsoft.gpg

# Register the Microsoft Product feed
sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list'

# Install PowerShell
sudo apt update && sudo apt install -y powershell

# Start PowerShell
pwsh

If the reader doesn’t have Windows Powershell™ installed on his or her system, I recommend getting it from microsoft.com and installing it– even if you don’t think you’d ever use most of the features it offers.

Since authoring a brief bit on Powershell, back in 2008, I’ve been using it more and more frequently. As I continue using Powershell, I find different– usually more efficient, and more powerful– ways of administering the system, or even completing simple tasks like file management.

Here, I offer a snippet– I admit– for private reference, but a passer-by might find it useful. The command might present some insight into the nature of Powershell, for anyone who is unfamiliar with it, or its many versatile functions.

I hope to revisit the topic, and elaborate upon it with some of the many notes I have regarding Powershell™, as I’ve compiled bits of commands in plain text files for my own refernce.

Examine Windows Services using .NET Powershell

I wrote the following command as part of my investigation into MySQL failing to run as a service (the typical installation setup) on a Windows XP system.

$wsl=(get-wmiobject win32_service -filter "name='WslService'")
$wsl | format-list *

$wsl | format-table

Using this simple command, I learned enough about the configuration of my system to identify what steps I needed to take in order to repair the problem. After a few minutes, MySQL was running as desired. (note: updated 08/2023 – changed the -filter “name=” from mysql to WslService, and the variable from $mysql to $wsl).

How?


The following is a transcript of the Powershell commands I invoked, leading up to my resolution (aside from a few irrelevant bits removed).

$s = get-service schedule

set-service -inputobject $s -status stopped

Description
———–
These commands stop the Schedule service on the local computer.

The first command uses the Get-Service cmdlet to get the Schedule service. The command saves the service in the $s
variable.

The second command uses the Set-Service cmdlet to change the status of the Schedule service to “Stopped”. It uses t
he InputObject parameter to submit the service stored in the $s variable, and it uses the Status parameter to speci
fy the desired status.

RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113399
Get-Service
Start-Service
Stop-Service
Restart-Service
Resume-Service
Suspend-Service
New-Service

$mysql=(get-wmiobject win32_service -filter "name='mysql'")
$mysql 

ExitCode : 3
Name : MySQL
ProcessId : 0
StartMode : Auto
State : Stopped
Status : OK

$mysql | format-list *

Name : MySQL
Status : OK
ExitCode : 3
DesktopInteract : False
ErrorControl : Normal
PathName :
ServiceType : Own Process
StartMode : Auto
__GENUS : 2
__CLASS : Win32_Service
__SUPERCLASS : Win32_BaseService
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_Service.Name=”MySQL”
__PROPERTY_COUNT : 25
__DERIVATION : {Win32_BaseService, CIM_Service, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER

: WIN-SYS-NAM-HERE
__NAMESPACE : root\cimv2
__PATH : \\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service.Name=”MySQL”
AcceptPause : False
AcceptStop : False
Caption : MySQL
CheckPoint : 0
CreationClassName : Win32_Service
Description :
DisplayName : MySQL
InstallDate :
ProcessId : 0
ServiceSpecificExitCode : 0
Started : False
StartName : LocalSystem
State : Stopped
SystemCreationClassName : Win32_ComputerSystem
SystemName : WIN-SYS-NAM-HERE
TagId : 0
WaitHint : 0
Scope : System.Management.ManagementScope
Path : \\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service.Name=”MySQL”
Options : System.Management.ObjectGetOptions
ClassPath : \\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service
Properties : {AcceptPause, AcceptStop, Caption, CheckPoint…}
SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY…}
Qualifiers : {dynamic, Locale, provider, UUID}
Site :
Container :

 help wmiobject


Name Category Synopsis
—- ——– ——–
Get-WmiObject Cmdlet Gets instances of Windows Management Instrumentation (WMI) classes or in…
Remove-WmiObject Cmdlet Deletes an instance of an existing Windows Management Instrumentation (W…
using cmd.exe, executed the following command:

sc delete mysql
[SC] DeleteService SUCCESS

after launching MySQL Instance Config Wizard, and reconfiguring the service, the changes are reflected in the .NET Powershell Windows Management Instrumentation Object (aka. wmiobject) properties for the MySQL Server instance– most remarkably– at the PathName property:

Name :MySQL
Status :OK
ExitCode :0
DesktopInteract :False
ErrorControl :Normal
PathName :“C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt” –defaults-file=”C:\Program Files\MySQL\MySQL Server 5.0\my.ini” MySQL
ServiceType :Own Process
StartMode :Auto
__GENUS :2
__CLASS :Win32_Service
__SUPERCLASS :Win32_BaseService
__DYNASTY :CIM_ManagedSystemElement
__RELPATH :Win32_Service.Name=”MySQL”
__PROPERTY_COUNT :25
__DERIVATION :{Win32_BaseService, CIM_Service, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER :WIN-SYS-NAM-HERE
__NAMESPACE :root\cimv2
__PATH :\\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service.Name=”MySQL”
AcceptPause :True
AcceptStop :True
Caption :MySQL
CheckPoint :0
CreationClassName :Win32_Service
Description : 
DisplayName :MySQL
InstallDate : 
ProcessId :5132
ServiceSpecificExitCode :0
Started :True
StartName :LocalSystem
State :Running
SystemCreationClassName :Win32_ComputerSystem
SystemName :WIN-SYS-NAM-HERE
TagId :0
WaitHint :0
Scope :System.Management.ManagementScope
Path :\\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service.Name=”MySQL”
Options :System.Management.ObjectGetOptions
ClassPath :\\WIN-SYS-NAM-HERE\root\cimv2:Win32_Service
Properties :{AcceptPause, AcceptStop, Caption, CheckPoint…}
SystemProperties :{__GENUS, __CLASS, __SUPERCLASS, __DYNASTY…}
Qualifiers :{dynamic, Locale, provider, UUID}
Site : 
Container :