0

The first time I open and run the program it installs Choco but will not install the applications. If I close the application and run it again, it will then install the applications. My guess is the WinForm doesn't know it can use the choco command? Is there a way to have the application refresh its system?

When installing Chocolatey it does display this: You may need to shut down and restart powershell and/or consoles first prior to using choco.

So I might just be SOL, but I figured I couldnt be the first person to attempt to do this type of thing.

Below is my code for installing Chocolatey and then installing a list of applications using it.

Sub InstallChocoApps()
    RunCmd("@""%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"" -NoProfile -ExecutionPolicy Bypass -Command ""iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"" && SET ""PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin""", "", False, True)

    Dim Packages() As String = {"notepadplusplus.install", "7zip.install", "firefox", "googlechrome", "putty.install", "sumatrapdf.install", "vlc"}
        For Each p In Packages
            RunCmd("choco install " & p & " --force -y --no-progress", "", False, True)
        Next
End Sub

Sub RunCmd(command As String, arguments As String, permanent As Boolean, display As Boolean)
        Try
            Dim p As Process = New Process()
            Dim pi As ProcessStartInfo = New ProcessStartInfo()
            pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
            pi.FileName = "cmd.exe"
            pi.WindowStyle = ProcessWindowStyle.Hidden
            pi.CreateNoWindow = True
            pi.Verb = "runas"
            pi.UseShellExecute = False
            p.StartInfo = pi
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.Start()
            p.WaitForExit()
            p.Close()


        Catch ex As Exception
            LogData(ex.ToString())
        End Try
    End Sub
Landmine
  • 1,759
  • 6
  • 39
  • 60

1 Answers1

1

Try installing Chocolatey and the packages in one call to RunCmd(). Either that or reference the absolute path of the Chocolatey executable when running the choco command. I believe right now it's C:\temp\MyChocolatey\bin\choco.exe.

arjabbar
  • 6,044
  • 4
  • 30
  • 46
  • Great idea to call it manually after the install. Thank you, sir! – Landmine Oct 13 '17 at 21:16
  • 1
    "%ALLUSERSPROFILE%\chocolatey\bin\choco.exe" is where you are going to find it. – ferventcoder Oct 16 '17 at 15:41
  • 1
    Yea, what ferventcoder said. Pretty sure he's a reliable source on Chocolatey. Not sure why it's in C:\temp\MyChocolatey\ on my work computer, but on my home computer it's under C:\ProgramData\chocolatey\. Same version and everything. – arjabbar Oct 22 '17 at 01:12