Created
March 3, 2021 01:28
-
-
Save karenpayneoregon/a239e27121fb1190a941f568da7a1f96 to your computer and use it in GitHub Desktop.
Basic code sample showing how to start a process hidden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Infer On | |
Public Class PowerShellOperations | |
''' <summary> | |
''' Get this computer's IP address | |
''' </summary> | |
''' <returns></returns> | |
Public Shared Async Function GetIpAddress() As Task(Of String) | |
Const fileName As String = "ipPower.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 ipinfo.io/ip", | |
.CreateNoWindow = True | |
} | |
Using process As System.Diagnostics.Process = System.Diagnostics.Process.Start(start) | |
Using reader = process.StandardOutput | |
process.EnableRaisingEvents = True | |
Dim ipAddressResult = reader.ReadToEnd() | |
Await File.WriteAllTextAsync(fileName, ipAddressResult) | |
Await process.WaitForExitAsync() | |
Return Await File.ReadAllTextAsync(fileName) | |
End Using | |
End Using | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment