1

Relatively new to C# and I am trying to remake a powershell application I built with winforms in which I made functionality to auto-populate a combobox with all local machines, and when selected it will show the relevant services for my companies application and whether the service was running and what it's startup type was. I made buttons that allowed for stop, start, restart, enable automatic and disable. I would like to translate this into a C# rebuild but I am struggling to find the C# equivalent of powershell's Set-Process and Get-Process, which allow, very easily, to get the status and change the status of services across the LAN without any special remote session being made.

I also used PsTools to close and restart our applications remotely from this app, and I would love to learn how to achieve the functionality of PsTools with C# and .NET.

My research has shown me that some of my questions can be answered in the System.Diagnostics, System.Management, and System.DirectoryServices namespaces, but when I put the latter two into a project in Visual Studio 2022 they come up as invalid namespaces.

  • Please share some of the code that you're using to try and solve this so that you will get more specific answers. – Bron Davies Jun 01 '22 at 04:23
  • Since they are not by default you'll need to add the latter two _assemblies_ as project references before you can make use of the same-named _namespaces_ they provide. If these computers are on an Active Directory domain you can use `System.DirectoryServices` types to query for computer members, but as far as remote process management on a specific computer you'll need the `System.Diagnostics.Process` .NET class or `Win32_Process` WMI class (via the `System.Management.ManagementObjectSearcher` or `System.Management.ManagementObject` .NET classes.) – Lance U. Matthews Jun 01 '22 at 04:24
  • > when I put the latter two into a project ... they come up as invalid namespaces. They are not recognized because your project doesn't have the references added yet. Depending on the version of Visual Studio you are using this process varies slightly but generally is done as described here: https://docs.microsoft.com/en-us/visualstudio/ide/managing-references-in-a-project?view=vs-2022 Once you add the references to the project your code will recognize the imported namespaces. https://i.stack.imgur.com/RCDIW.png – Bron Davies Jun 01 '22 at 04:33
  • I am seeking help for C# equivalencies so I have no code other than a lot of dead ened research, but the equivalent code from the powershell program are similar to (Get-Service -Name Spooler).Status //returns "Running" Set-Service -Name Spooler -ComputerName //computer2 -Status Stopped // Stops spooler service on network psexec //computer2 -i 2 -c -d "c:\program files\program.exe" // opens program.exe on network machine – Eric Johnson Jun 01 '22 at 04:35
  • .NET Framework is different from .NET. Some of the things that one added as references in .NET Framework, need to be installed as a NuGet package in .NET. Install the NuGet package: `System.Management`. In VS menu, click `View`, select `Solution Explorer`. In Solution Explorer, right-click your project name, then select `Manage NuGet Packages...`. Click `Browse`. Type: `System.Management`. Click `Install`. If a prompt appears, click `OK`. After the NuGet package is installed, add `using System.Management;` to your desired .cs file (ex: Form1.cs) – user9938 Jun 01 '22 at 14:36
  • For `System.Diagnostics.Process`, the following may be helpful: https://stackoverflow.com/questions/71343454/how-do-i-use-redirectstandardinput-when-running-a-process-with-administrator-pri/71344930#71344930 and https://stackoverflow.com/questions/71343454/how-do-i-use-redirectstandardinput-when-running-a-process-with-administrator-pri/71344930#71344930 ; – user9938 Jun 01 '22 at 16:56
  • For PowerShell, the following may be helpful: https://stackoverflow.com/questions/58211358/how-to-automate-either-powershell-or-powershell-core-for-same-machine/58211901#58211901 and https://stackoverflow.com/questions/70403581/execute-a-continuously-running-powershell-command-and-get-result-into-form – user9938 Jun 01 '22 at 16:58
  • For WMI, the following may be helpful: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-service and https://stackoverflow.com/questions/71362463/how-to-get-the-ram-name/71388371#71388371 – user9938 Jun 01 '22 at 17:01

0 Answers0