Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created March 3, 2021 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpayneoregon/a239e27121fb1190a941f568da7a1f96 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/a239e27121fb1190a941f568da7a1f96 to your computer and use it in GitHub Desktop.
Basic code sample showing how to start a process hidden
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