Option Explicit
Enum CMDModeOptions
optDirectCMD = 1
optClipboard = 2
optTempFile = 3
End Enum
Sub getProxyPowerShell()
Dim cmd As String
Dim result As String
cmd = "(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer"
result = cliCmdExecute(cmd, True, optDirectCMD)
ActiveCell.Value = result
ActiveCell.Offset(1).Select
Debug.Print "proxy: " & Trim(Replace(Replace(result, Chr(10), ""), Chr(13), ""))
cmd = "(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyEnable"
result = cliCmdExecute(cmd, True, optDirectCMD)
ActiveCell.Value = result
ActiveCell.Offset(1).Select
Debug.Print "status: " & Trim(Replace(Replace(result, Chr(10), ""), Chr(13), ""))
End Sub
Function cliCmdExecute(ByVal cli_command As String, Optional usePowerShell As Boolean = False, Optional ByVal Mode As CMDModeOptions = CMDModeOptions.optTempFile) As String
If cli_command = "" Then Exit Function
Dim shell_prefix As String, shell_suffix As String
Dim Results As String, objShell As Object
Dim fullCommand As String
If usePowerShell Then
shell_prefix = "PowerShell -ExecutionPolicy Bypass -Outputformat Text -Command """
shell_suffix = """"
Else
shell_prefix = "cmd /C "
shell_suffix = ""
End If
fullCommand = shell_prefix & cli_command
Set objShell = CreateObject("Wscript.Shell")
Select Case Mode
Case optDirectCMD
Results = objShell.Exec(fullCommand).StdOut.ReadAll()
Case optClipboard
fullCommand = fullCommand & " | clip" & shell_suffix
objShell.Run fullCommand, 0, True
On Error Resume Next
With CreateObject("htmlfile")
Results = .ParentWindow.ClipboardData.GetData("text")
.ParentWindow.ClipboardData.clearData ("text")
End With
On Error Resume Next
Case optTempFile
Dim tmpfile As String: tmpfile = generateTempFileName
fullCommand = fullCommand & " *> " & tmpfile & shell_suffix
objShell.Run fullCommand, 0, True
On Error Resume Next
With CreateObject("Scripting.FileSystemObject")
If .GetFile(tmpfile).Size > 0 Then
Results = .OpenTextFile(tmpfile).ReadAll()
End If
.DeleteFile tmpfile
End With
On Error GoTo 0
Case Else
Exit Function
End Select
If Len(Results) = 0 Then Exit Function
cliCmdExecute = Results
Set objShell = Nothing
End Function
Function generateTempFileName(Optional str1 As String, Optional strSeparator As String = "_") As String
On Error Resume Next
str1 = Trim(str1)
If Len(str1) = 0 Then str1 = "tmpOut"
generateTempFileName = TrailingSlash(Environ("temp")) & _
str1 & strSeparator & _
Format(Now, "yyyymmdd" & strSeparator & "hhnnss") & strSeparator & _
Int(Rnd * 100000) & ".txt"
End Function
Public Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0& Then
If Right(varIn, 1&) = Application.PathSeparator Then TrailingSlash = varIn Else TrailingSlash = varIn & Application.PathSeparator
End If
End Function