none
need help to automate SFC and DISM commands using visual basic RRS feed

  • Question

  • Hello, 

    I am trying to generate an .exe program that could open the command prompt as administrator and run the sfc /scannow command. 

    I also would like to do the same for the DISM /Online /Cleanup-Image /RestoreHealth command.

    I code using visual basic.

    I can get as far as getting the "Windows Resource Protection could not start the repair service." message. 

    I already changed the level to "requireAdministrator" under the "Project Properties" >  "View Windows Settings" menu.

    I also run the .exe as administrator

    Any ideas on how to get this to work ?

    Thank you!

    Tuesday, December 18, 2018 9:27 PM

All replies

  • Hi,

    try the way:

    1.Enter the command SERVICES.MSC in the Run window.

    2.In the service window that opens, find the Windows Modules Installer,right click on the service item and select the "Properties" menu item in the pop-up menu.

    3.Then in the properties window that opens, click the "Start" button to start the service.

    4.Then click on the "Startup Type" drop-down menu and click on the "Auto" menu item in the pop-up menu. Finally click the OK button.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 19, 2018 2:36 AM
  • Hello, and thank you for taking the time to respond.

    I don't think we are on the same page. As previously mentioned, I am trying to find out how to generate an .exe program that could open the command prompt as administrator and run the sfc /scannow command. 
    Wednesday, December 19, 2018 10:32 PM
  • Hi,

    Do you mean that the clicked exe program runs directly as an administrator?

    1.New module writes the following code

    Module Module1
        Public Sub Main(ByVal Args As String())
            Dim identity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
            Application.EnableVisualStyles()
            Dim principal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(identity)
    
            If principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) Then
                Application.EnableVisualStyles()
                Application.Run(New Form1())
            Else
                Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
                startInfo.FileName = System.Windows.Forms.Application.ExecutablePath
                startInfo.Arguments = String.Join(" ", Args)
                startInfo.Verb = "runas"
                System.Diagnostics.Process.Start(startInfo)
                System.Windows.Forms.Application.[Exit]()
            End If
        End Sub
    
    End Module
    

    2.Open the My Project-application, remove the Enable Application Framework check box, and launch the object selection Sub Main.

    3.Form1 Window code:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
            startInfo.FileName = "cmd.exe"
            startInfo.Arguments = "/c C:\Windows\System32\cmd.exe"
            startInfo.RedirectStandardInput = True
            startInfo.RedirectStandardOutput = True
            startInfo.RedirectStandardError = True
            startInfo.UseShellExecute = False
            startInfo.Verb = "RunAs"
            Dim process As Process = New Process()
            process.StartInfo = startInfo
            process.Start()
            process.StandardInput.WriteLine("sfc /scannow")
            process.StandardInput.WriteLine("exit")
            Dim strRst As String = process.StandardOutput.ReadToEnd()
            'Bootinitext.AppendText(vbLf & strRst)
            process.WaitForExit()
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 20, 2018 7:14 AM
  • Hi Alex, 

    I tried the code and my PC got stuck in a loop. I had to restart the PC...

    Monday, January 7, 2019 10:44 PM
  • I'd make a few changes to the code posted above:

    the /K leaves the command window open so you can see the results

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
            startInfo.FileName = "cmd.exe"
            startInfo.Arguments = "/K sfc /scannow"
            'startInfo.RedirectStandardInput = True
            'startInfo.RedirectStandardOutput = True
            'startInfo.RedirectStandardError = True
            'startInfo.UseShellExecute = False
            startInfo.Verb = "RunAs"
            Dim process As Process = New Process()
            process.StartInfo = startInfo
            process.Start()
            'process.StandardInput.WriteLine("sfc /scannow")
            'process.StandardInput.WriteLine("exit")
            'Dim strRst As String = process.StandardOutput.ReadToEnd()
            'Bootinitext.AppendText(vbLf & strRst)
            'process.WaitForExit()
        End Sub

    Results:


    • Edited by Devon_Nullman Tuesday, January 8, 2019 5:57 AM added /K info
    Tuesday, January 8, 2019 5:56 AM