Results 1 to 4 of 4

Thread: Powrshell command and output

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Powrshell command and output

    I need some help, I'm trying to send this command

    Code:
    $computer = Get-ADComputer WS082
     Get-ADObject -Filter 'objectClass -eq "msFVE-RecoveryInformation"' -SearchBase $computer.DistinguishedName -Properties  msFVE-RecoveryPassword | Select msFVE-RecoveryPassword

    The command works in powershell but trying to get it to run in Visual studios 2019 and give an output in a textbox.

    I've tried this but I'm sure I've messed up somewhere. I'm new to powershell and VB.

    Code:
       Dim command As New PSCommand()
                command.AddScript("$computer = Get-ADComputer WS08")
                command.AddScript("Get-ADObject -Filter 'objectClass -eq ""msFVE-RecoveryInformation""' -SearchBase $computer.DistinguishedName -Properties  msFVE-RecoveryPassword | Select msFVE-RecoveryPassword")
                Dim powershell As Management.Automation.PowerShell = PowerShell.Create()
                powershell.Commands = command
                Dim results = powershell.Invoke()
                MsgBox(results.Item(0).ToString)
    thank you
    Brock

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Powrshell command and output

    If you have your PowerShell script send its output to a text file, then you can read in that text file and place the text into a textbox.

    Possibly useful link:

    https://social.msdn.microsoft.com/Fo...orum=vbgeneral

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Re: Powrshell command and output

    Quote Originally Posted by jdc2000 View Post
    If you have your PowerShell script send its output to a text file, then you can read in that text file and place the text into a textbox.

    Possibly useful link:

    https://social.msdn.microsoft.com/Fo...orum=vbgeneral
    my issue Im not sure how to process this code

    Code:
     command.AddScript("Get-ADObject -Filter 'objectClass -eq ""msFVE-RecoveryInformation""' -SearchBase $computer.DistinguishedName -Properties  msFVE-RecoveryPassword | Select msFVE-RecoveryPassword")
    I think command.addscript is wrong but not sure what I should use to process the next command.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Powrshell command and output

    The cheap way is using Process.Start. Since you have one variable that can be done with string interpolation. The following is a working proof of concept which gets the current IP.

    Code:
    Imports System.IO
    
    Public Class Form1
     Public Function GetIpAddressSync(Optional argument As String = "ipinfo.io/ip") As String
            Const fileName = "ipPower_Sync.txt"
    
    
            If File.Exists(fileName) Then
                File.Delete(fileName)
            End If
    
            Dim start = New ProcessStartInfo With {
                        .FileName = "powershell.exe",
                        .UseShellExecute = False,
                        .RedirectStandardOutput = True,
                        .Arguments = $"Invoke-RestMethod {argument}",
                        .CreateNoWindow = True
                    }
    
    
            Using process As Process = Process.Start(start)
                Using reader = process.StandardOutput
    		
                    process.EnableRaisingEvents = True
    		
                    Dim ipAddressResult = reader.ReadToEnd()
    		
                    File.WriteAllText(fileName, ipAddressResult)
                    process.WaitForExit()
    		
                    Return File.ReadAllText(fileName)
                End Using
            End Using
        End Function
    
        Private Sub ExecuteButton_Click(sender As Object, e As EventArgs) Handles ExecuteButton.Click
            ResultsTextBox.Text = GetIpAddressSync()
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width