Forum Discussion

ixxo09's avatar
ixxo09
Copper Contributor
Nov 21, 2024

Strange chraracters in the powershell command PS_GetOutputFile("netsh wlan show interfaces")

I´m creating a sub that lay out in a textbox of a form with strange chrs. my sub is as follow:

Private Sub Form_Load()
    'Redondear las esquinas del formulario
    Call UISetRoundRect(Me, 40, False)
    
    'This sets an exact position using MoveSize if form´s parent is loaded, otherwiseccenter de form on screen
    If IsLoaded("frmDashBoard") Then DoCmd.MoveSize 15050, 2400 Else Call gfncCenterForm(Me)
    
    With Me
        'Encabezado de la información de la red Wi-Fi
        If Not IsNull(Me.OpenArgs) Then Me!lblInfo.Caption = "INFORMACIÓN " & Me.OpenArgs
        
        'Cargar la información de la red Wi-Fi
        !txtInfo.Value = PS_GetOutputFile("netsh wlan show interfaces")
        '!lblTitle.Caption = PS_GetOutputClipBoard("(Get-NetConnectionProfile).Name")
    End With
End Sub

 

 

 

 

 

Public Function PS_GetOutputFile(ByVal sPSCmd As String, _
                                Optional sTxtFile As String = vbNullString, _
                                Optional bDelTxtFile As Boolean = True) As String
    
    'If no Text file was specified create one in the Temp folder
    If sTxtFile = vbNullString Then sTxtFile = Environ$("temp") & "\PSTemp.txt"
    
    'Build the basic PowerShell command
    sPSCmd = "powershell -command " & sPSCmd
    
    'Add the Out-File so the output generates a text file
    sPSCmd = sPSCmd & " | Out-File '" & sTxtFile & "' -Encoding Default"

    'Run the PowerShell command
    CreateObject("WScript.Shell").Run sPSCmd, 0, True
    
    'Retrieve the content of the generated Text file
    With CreateObject("Scripting.FileSystemObject")
        'Read the contents of the text file into memory
        PS_GetOutputFile = .OpenTextFile(sTxtFile).ReadAll()
        'Delete the text file if so desired
        If bDelTxtFile = True Then .DeleteFile sTxtFile
    End With
    
End Function

Public Function PS_GetOutputClipBoard(ByVal sPSCmd As String) As String
    'Setup the powershell command properly
    sPSCmd = "powershell -command " & sPSCmd & "|clip"
    'Execute the command which is being pushed to  the clipboard
    CreateObject("WScript.Shell").Run sPSCmd, 0, True
    'Get an instance of the clipboard to capture the save value
    With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        .GetFromClipboard
        PS_GetOutputClipBoard = .GetText(1)
    End With
End Function

the result was:

Hay 1 interfaz en el sistema:

 

    Nombre                  : Wi-Fi

    Descripci+¦n            : Realtek 8822CE Wireless LAN 802.11ac PCI-E NIC

    GUID                  :

    Direcci+¦n      :

    Tipo de interfaz        : Principal

    Estado                  : conectado

    SSID                  : Redmi Note 12 Pro 5G

    AP BSSID             

    Banda                : 2,4-áGHz

    Canal: 6

    Tipo de red            : Infraestructura

    Tipo de radio          : 802.11n

    Autenticaci+¦n          : WPA2-Personal

    Cifrado                : CCMP

    Modo de conexi+¦n      : Conexi+¦n autom+ítica

    Velocidad de recepci+¦n (Mbps)  : 144.4

    Velocidad de transmisi+¦n (Mbps) : 144.4

    Seal                          : 100%

    Perfil                : Redmi Note 12 Pro 5G

    MSCS de QoS configurado: 0

    Asignaci+¦n de QoS configurada: 0

    Asignaci+¦n de QoS permitida por la directiva : 0

 

    Estado de la red hospedada: No disponible

this character +¦  it suposed to be a vocal with acent ´cause is in spanish language, also could be ñáéíóú

Could someone help me please!!

Thanks in advance.

 

  • What if you try a different approach?

    Function RunCmd() As String
        Dim sCmd As String
        
        sCmd = "netsh wlan show interfaces | clip"
        CreateObject("Wscript.Shell").Run "cmd /c " & sCmd, 0, True
        DoEvents
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            .GetFromClipboard
            RunCmd = .GetText
        End With
    End Function

     

    Don't forget, you need to use an appropriate font to display the results and the VBE can't handle unicode.

     

    You may also like to read the following discussion which talks about PowerShell encoding: Displaying Unicode in PowerShell.  If you search you find many more discussions on the subject.

     

     

    As a side note, if you take other people's work (my PowerShell procedures) you could at least have the decency to keep the header/copyright notice.  I do all the work and you can't even show that minimum level of respect!

  • arnel_gp's avatar
    arnel_gp
    Steel Contributor

    here is another code and without you needing to add attribution:

    Private Sub Form_Load()
        Dim content As String
        Dim cmd As String
        Dim outputfile As String
        
        outputfile = Environ$("temp") & "\PSTemp.txt"
        
        cmd = "cmd.exe /c netsh wlan show interfaces > """ & outputfile & """"
        
        
         'Redondear las esquinas del formulario
        Call UISetRoundRect(Me, 40, False)
        
        'This sets an exact position using MoveSize if form´s parent is loaded, otherwiseccenter de form on screen
        If IsLoaded("frmDashBoard") Then DoCmd.MoveSize 15050, 2400 Else Call gfncCenterForm(Me)
        
        With Me
            'Encabezado de la información de la red Wi-Fi
            If Not IsNull(Me.OpenArgs) Then Me!lblInfo.Caption = "INFORMACIÓN " & Me.OpenArgs
            
            Shell cmd, vbHide
            
            
            'Cargar la información de la red Wi-Fi
            !txtInfo.Value = ReadTextFileContent(outputfile)
        
        End With
    End Sub
    
    ' chatgpt
    Public Function ReadTextFileContent(ByVal filepath As String) As String
        Dim fso As Object
        Dim fileContent As String
        Dim textFile As Object
        
        
        ' Create a FileSystemObject
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        ' Check if the file exists
        If fso.FileExists(filepath) Then
            ' Open the file as a text stream
            Set textFile = fso.OpenTextFile(filepath, 1) ' 1 = ForReading
            
            ' Read the entire file content
            fileContent = textFile.ReadAll
            
            ' Close the file
            textFile.Close
            
        Else
            MsgBox "File not found: " & filepath, vbExclamation, "Error"
        
        End If
        
        ' Clean up
        Set textFile = Nothing
        Set fso = Nothing
        
        ReadTextFileContent = fileContent
    End Function

     

Resources