Run powershell in C#
C / C++ / MFC
1
Posts
1
Posters
0
Views
1
Watching
-
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?
// 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 results = ps.Invoke(); runspace.Close(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { text\_LastWinUpd.Text = obj.ToString(); } } // END