Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Run powershell in C#

Run powershell in C#

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpcomwindows-adminhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jwradhe
    wrote on last edited by
    #1

    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
    
    L Richard DeemingR 2 Replies Last reply
    0
    • J jwradhe

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Quote:

      I understand that im doing wrong

      Yes, this forum is for C/C++ questions. What happens when you run the powershell commands in PowerShell and not via C#?

      1 Reply Last reply
      0
      • J jwradhe

        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
        
        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        J 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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

          J Offline
          J Offline
          jwradhe
          wrote on last edited by
          #4

          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

          Richard DeemingR D 2 Replies Last reply
          0
          • J jwradhe

            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

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            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

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            J 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              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

              J Offline
              J Offline
              jwradhe
              wrote on last edited by
              #6

              2021-09-20 is the result from PS command

              Richard DeemingR 1 Reply Last reply
              0
              • J jwradhe

                2021-09-20 is the result from PS command

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                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

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • J jwradhe

                  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

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups