Link to home
Start Free TrialLog in
Avatar of TonyElam
TonyElam

asked on

powershell pssession

I have set up a windows form to open a dialogue box to allow admins to initiate a pssession on a remote system.  I need help writing the actual function that will initiate the pssession on the remote system to code I have so far is below

function pssession {



}



[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form = New-Object System.Windows.Forms.Form

$Form.width = 500
$Form.height = 350
$Form.Text = "Map Drive"
$Form.backcolor = "#5D8AA8"
$Form.maximumsize = New-Object System.Drawing.Size(500, 350)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})

$start = new-object System.Windows.Forms.Button
$start.Location = new-object System.Drawing.Size(185,200)
$start.Size = new-object System.Drawing.Size(120,30)
$start.Text = "START"
$start.FlatAppearance.MouseOverBackColor = [System.Drawing.Color]::FromArgb(255, 255, 192);
$start.ImageAlign = [System.Drawing.ContentAlignment]::MiddleLeft;
$start.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$start.Add_Click({mapdrive})

$Form.Controls.Add($start)

$RemoteMachine = new-object System.Windows.Forms.TextBox
$RemoteMachine.Location = new-object System.Drawing.Size(195,60)
$RemoteMachine.Size = new-object System.Drawing.Size(100,20)

$Form.Controls.Add($RemoteMachine)

$label = new-object System.Windows.Forms.Label
$label.Location = new-object System.Drawing.Size(175,10)
$label.size = new-object System.Drawing.Size(200,50)
$label.Font = new-object System.Drawing.Font("Microsoft Sans Serif",12,[System.Drawing.FontStyle]::Bold)
$label.Text = "Remote System"
$Form.Controls.Add($label)




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


I am still quite new to powershell and any help would be appreciated.
Thanks
Avatar of Qlemo
Qlemo
Flag of Germany image

What's the action you want to perform? Just start an interactive remote session?
Avatar of TonyElam
TonyElam

ASKER

yes
You should replace
    $Form.Text = "Map Drive"
with
   $Form.Text = "Connect to Machine"

and
   $start.Add_Click({mapdrive})
with
   $start.Add_Click({enter-pssession $RemoteMachine.Text})


The first change is a nuance only, but the second one is important. That defines which code will be executed if you press the START button.
it is working but not quite as expected. here are the steps I am using
1 openpowershell
2 run the script
3 enter remote machine name and click start
4 i am having to close the open form and then the connection establishes in the open powershell console

my original goal was to be able to double click the script and have it open, enter the name of the remote systen, click start and have a powershell window open to allow an interactive pssession window.  I might be trying for the impossible though.  I am still pretty darn new to all of the.  At any rate thank you for your help thus far:)
current code i am using is

function pssession {



}



[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form = New-Object System.Windows.Forms.Form

$Form.width = 500
$Form.height = 350
$Form.Text = "Connect to Machine"
$Form.backcolor = "#5D8AA8"
$Form.maximumsize = New-Object System.Drawing.Size(500, 350)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})

$start = new-object System.Windows.Forms.Button
$start.Location = new-object System.Drawing.Size(185,200)
$start.Size = new-object System.Drawing.Size(120,30)
$start.Text = "START"
$start.FlatAppearance.MouseOverBackColor = [System.Drawing.Color]::FromArgb(255, 255, 192);
$start.ImageAlign = [System.Drawing.ContentAlignment]::MiddleLeft;
$start.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
 $start.Add_Click({enter-pssession $RemoteMachine.Text})

$Form.Controls.Add($start)

$RemoteMachine = new-object System.Windows.Forms.TextBox
$RemoteMachine.Location = new-object System.Drawing.Size(195,60)
$RemoteMachine.Size = new-object System.Drawing.Size(100,20)

$Form.Controls.Add($RemoteMachine)

$label = new-object System.Windows.Forms.Label
$label.Location = new-object System.Drawing.Size(175,10)
$label.size = new-object System.Drawing.Size(200,50)
$label.Font = new-object System.Drawing.Font("Microsoft Sans Serif",12,[System.Drawing.FontStyle]::Bold)
$label.Text = "Remote System"
$Form.Controls.Add($label)




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

The "brute-force" method is to use this line
  $start.Add_Click({ $Form.Close(); enter-pssession $RemoteMachine.Text})
It will close the dialog, and then issue the command.
perfect, only remaining issue is the ps1 file will not open the form when double clicked.  i have to call it from a powershell session
if iright click and choose run with powershell it opens the form, allows me to enter a name and click start but then it closes form, and ps windows
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Marvolus help works exactly as I envisioned now