Run powershell in C#
-
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
-
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
-
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
As the other Richard said, you've posted this in the wrong forum. But you don't need to invoke Powershell in order to create and use a COM object. You can do that directly from C#:
Type t = Type.GetTypeFromProgID("Microsoft.Update.AutoUpdate");
dynamic a = Activator.CreateInstance(t);
DateTime lastSuccessfulUpdate = a.Results.LastInstallationSuccessDate;
text_LastWinUpd.Text = lastSuccessfulUpdate.ToString();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As the other Richard said, you've posted this in the wrong forum. But you don't need to invoke Powershell in order to create and use a COM object. You can do that directly from C#:
Type t = Type.GetTypeFromProgID("Microsoft.Update.AutoUpdate");
dynamic a = Activator.CreateInstance(t);
DateTime lastSuccessfulUpdate = a.Results.LastInstallationSuccessDate;
text_LastWinUpd.Text = lastSuccessfulUpdate.ToString();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi! Yeah if possible i want it in C# only. I trid the code but i gives this: 0001-01-01 00:00:00 +00:00
What do you get when you run the Powershell code in Powershell on the same computer?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What do you get when you run the Powershell code in Powershell on the same computer?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The Powershell script and the C# code return the same result for me. Are you definitely running them both as the same user on the same computer?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi! Yeah if possible i want it in C# only. I trid the code but i gives this: 0001-01-01 00:00:00 +00:00
Works for me:
private DateTime GetLastUpdateSuccessDate()
{
Type t = Type.GetTypeFromProgID("Microsoft.Update.AutoUpdate");
dynamic a = Activator.CreateInstance(t);
DateTime lastUpdate = a.Results.LastInstallationSuccessDate;return lastUpdate;
}
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak