Run powershell in C#

jwradhe

Member
Joined
Sep 23, 2021
Messages
7
Programming Experience
Beginner
Hi!
I am pretty new to this, but im trying to present when the latest winupdate was done on computer, and only found in powershell to present that, and i tried to run it in C#, but no error and no result.
I understand that im doing wrong, but what is the best way to do this?

C#:
// Get Latest Windows Update
        private void LastWinUpd()
        {
           
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();

            PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;

            //ps.Commands.AddScript("gwmi win32_quickfixengineering | Select-Object -expandProperty 'installedon' | sort installedon -desc | Select-Object -First 1 | Get-Date -Format 'yyyy - MM - dd K'");

            ps.AddCommand("$a = (New - Object - com 'Microsoft.Update.AutoUpdate').Results");
            ps.AddCommand("$a.LastInstallationSuccessDate | Get - Date - Format 'yyyy-MM-dd K'");

            Collection<PSObject> results = ps.Invoke();

            runspace.Close();

            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                text_LastWinUpd.Text = obj.ToString();
            }
           
        }
        // END
 
Last edited by a moderator:
So what actually happens? Do you end up with a Collection<PSObject> that's empty or something else? Are you able to successfully get the information you want in PowerShell itself? If not and you want help to fix the PowerShell code then this is a PowerShell question rather than a C# question. If the C# code is correct but the PowerShell code is not then there's nothing to be done from the C# end.
 
PPowershell part works perfect, but i dont get it in the C# code.

if i use this part instead the 2 ps.AddCommand i got result. But wrong date.
C#:
 //ps.Commands.AddScript("gwmi win32_quickfixengineering | Select-Object -expandProperty 'installedon' | sort installedon -desc | Select-Object -First 1 | Get-Date -Format 'yyyy - MM - dd K'");
 
Last edited by a moderator:
This is not a C# problem nor a PowerShell problem. This the problem about the difference between WMI vs the AutoUpdate object model.
 
Moving out of WinForms and into some other more appropriate subforum...
 
Hello,
The good answer has already been given. That being said, there are some treatments for which you find easierly how to do in PowerShell than in C#.
If this is the case, and provided you need no user input in PowerShell, you can serialize the results with Export-csv,, and then in C# you can call it with something like that :
Calling a PowerShell script from C#:
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("PowerShell", @"D:\Scripts\ReduceDirWindows.ps1");
            p.StartInfo.CreateNoWindow = true;
            p.Start();

And then, you can open the CSV file with a StreamReader, followed by a code like
Splitting a line according to a separator:
string[] spl = line.Split(';');

For sure, this is not a clean solution. But it can allow you to obtain the results quickly, and when you have the time to investigate you search how to do everything in the same language.
 

Latest posts

Back
Top Bottom