-
Oct 6th, 2021, 04:49 PM
#1
Thread Starter
New Member
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
-
Oct 6th, 2021, 06:45 PM
#2
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
-
Oct 7th, 2021, 11:09 AM
#3
Thread Starter
New Member
Re: Powrshell command and output
Originally Posted by jdc2000
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.
-
Oct 7th, 2021, 12:15 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|