?
Solved

powershell pssession

Posted on 2011-05-12
10
Medium Priority
?
2,074 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:TonyElam
  • 6
  • 4
10 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 35755382
What's the action you want to perform? Just start an interactive remote session?
0
 

Author Comment

by:TonyElam
ID: 35755593
yes
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35755943
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.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:TonyElam
ID: 35756161
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:)
0
 

Author Comment

by:TonyElam
ID: 35756190
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()

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 35756320
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.
0
 

Author Comment

by:TonyElam
ID: 35756531
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
0
 

Author Comment

by:TonyElam
ID: 35756567
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
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 35759876
That's the best I can do here. It spawns a new powershell process, which then runs the remote shell, without blocking the dialog box - you can hence open more than one remote session.
The only issue is that you need to exit twice - one for the remote session, another one for powershell.
function pssession
{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    Set-StrictMode –version Latest
    
    $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({$pc = $RemoteMachine.Text; Start-Process powershell -ArgumentList "-noexit -command Enter-PSSession -Computer $pc"})
 
    $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()
}

pssession

Open in new window

0
 

Author Closing Comment

by:TonyElam
ID: 35769308
Marvolus help works exactly as I envisioned now
0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question