I think this is worth noting for me creating a custom solution for Sandboxing an Environment full of domain admins.

Here are the interop’s if you need them, place them in the $dllpath that you choose.

Interop.MSTSCLib

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

#Must run in x32 due to LoadAsm and the MSTSCfile
#Define Butten Click Function

$dllpath = 'C:\mstsc'
[system.reflection.Assembly]::LoadFrom("$dllpath\AxInterop.MSTSCLib.dll")
[system.reflection.Assembly]::LoadFrom("$dllpath\MSTSCLib.dll")
$rdp = New-Object AxMSTSCLib.AxMsRdpClient6NotSafeForScripting

[System.Management.Automation.ScriptBlock]$handleconnection = {
    # switch panels
    Write-Host "TADA!"
    #to stop run 
    $timer.stop() 
    #cleanup 
    Unregister-Event thetimer
    }

Function DisplayHelloWorldText{
$Form.Controls.Add($Label)
$rdp.Name = "MyPowerShellRDP" 
$rdp.Enabled = "true" 

$ScreenBounds = [Windows.Forms.SystemInformation]::VirtualScreen

$rdp.DesktopWidth = $ScreenBounds.Width
$rdp.DesktopHeight = $ScreenBounds.Height
$rdp.AdvancedSettings2.DisplayConnectionBar = 'true' 

$rdp.AdvancedSettings2.DisplayConnectionBar = 'true' 
$rdp.AdvancedSettings2.EnableCredSspSupport = "true" 
$rdp.RemoteProgram.RemoteProgramMode = 'true'
$rdp.AdvancedSettings7.SmartSizing = 'true'
$rdp.AdvancedSettings7.PublicMode = 'false'
$rdp.AdvancedSettings7.AuthenticationLevel = 0
register-objectEvent -InputObject $rdp -EventName "OnConnected" -Action { Write-Host "<TIMER>" }
$rdp.ConnectingText = 'Connecting...' 
$rdp.DisconnectedText = "Disconnected" 

$rdp.Server = "PutServerNameHere"
$rdp.UserName = ""
$rdp.AdvancedSettings2.RDPPort = "3389"
$rdp.AdvancedSettings2.ClearTextPassword = "" 
$rdp.Connect()

$timer = new-object timers.timer 

$action = {write-host "Timer Elapse Event: $(get-date -Format ‘HH:mm:ss’)"} 
$timer.Interval = 3000 #3 seconds  

#Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier thetimer -Action $handleconnection

#$timer.start()

#to stop run 
#$timer.stop() 
#cleanup 
#Unregister-Event thetimer

}

Function SpawnApp{
    $rdp.RemoteProgram.ServerStartProgram("cmd.exe", "", "%SYSTEMROOT%", 'True', "", 'False')
    $rdp.RemoteProgram.ServerStartProgram("calc.exe", "", "%SYSTEMROOT%", 'True', "", 'False')
}

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "My simple Form"
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "This form is very simple."
$Label.AutoSize = $True
$Form.Controls.Add($Label)

#Create button
$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(50, 50)
$Button1.Size = new-object System.Drawing.Size(80,20)
$Button1.Text = "Button"
$Button1.Add_Click({DisplayHelloWorldText})
$Form.Controls.Add($Button1)

#Create button
$Button2 = new-object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Size(50, 70)
$Button2.Size = new-object System.Drawing.Size(80,20)
$Button2.Text = "Spawn"
$Button2.Add_Click({SpawnApp})
$Form.Controls.Add($Button2)

$rdp.Dock = 'Fill' 
$Form.Controls.Add($rdp)

$Form.Add_Shown({$Form.Activate()})

$Form.ShowDialog()

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax