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.Assembl y]::LoadWi thPartialN ame("Syste m.Windows. Forms")
[System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.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.Butto n
$start.Location = new-object System.Drawing.Size(185,20 0)
$start.Size = new-object System.Drawing.Size(120,30 )
$start.Text = "START"
$start.FlatAppearance.Mous eOverBackC olor = [System.Drawing.Color]::Fr omArgb(255 , 255, 192);
$start.ImageAlign = [System.Drawing.ContentAli gnment]::M iddleLeft;
$start.FlatStyle = [System.Windows.Forms.Flat Style]::Fl at
$start.Add_Click({mapdrive })
$Form.Controls.Add($start)
$RemoteMachine = new-object System.Windows.Forms.TextB ox
$RemoteMachine.Location = new-object System.Drawing.Size(195,60 )
$RemoteMachine.Size = new-object System.Drawing.Size(100,20 )
$Form.Controls.Add($Remote Machine)
$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("Micro soft Sans Serif",12,[System.Drawing. FontStyle] ::Bold)
$label.Text = "Remote System"
$Form.Controls.Add($label)
$Form.Add_Shown({$Form.Act ivate()})
$Form.ShowDialog()
I am still quite new to powershell and any help would be appreciated.
Thanks
function pssession {
}
[System.Reflection.Assembl
[System.Reflection.Assembl
$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.Butto
$start.Location = new-object System.Drawing.Size(185,20
$start.Size = new-object System.Drawing.Size(120,30
$start.Text = "START"
$start.FlatAppearance.Mous
$start.ImageAlign = [System.Drawing.ContentAli
$start.FlatStyle = [System.Windows.Forms.Flat
$start.Add_Click({mapdrive
$Form.Controls.Add($start)
$RemoteMachine = new-object System.Windows.Forms.TextB
$RemoteMachine.Location = new-object System.Drawing.Size(195,60
$RemoteMachine.Size = new-object System.Drawing.Size(100,20
$Form.Controls.Add($Remote
$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("Micro
$label.Text = "Remote System"
$Form.Controls.Add($label)
$Form.Add_Shown({$Form.Act
$Form.ShowDialog()
I am still quite new to powershell and any help would be appreciated.
Thanks
What's the action you want to perform? Just start an interactive remote session?
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-ps session $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.
$Form.Text = "Map Drive"
with
$Form.Text = "Connect to Machine"
and
$start.Add_Click({mapdrive
with
$start.Add_Click({enter-ps
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.
ASKER
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:)
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:)
ASKER
current code i am using is
function pssession {
}
[System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.Windows. Forms")
[System.Reflection.Assembl y]::LoadWi thPartialN ame("Syste m.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.Butto n
$start.Location = new-object System.Drawing.Size(185,20 0)
$start.Size = new-object System.Drawing.Size(120,30 )
$start.Text = "START"
$start.FlatAppearance.Mous eOverBackC olor = [System.Drawing.Color]::Fr omArgb(255 , 255, 192);
$start.ImageAlign = [System.Drawing.ContentAli gnment]::M iddleLeft;
$start.FlatStyle = [System.Windows.Forms.Flat Style]::Fl at
$start.Add_Click({enter-ps session $RemoteMachine.Text})
$Form.Controls.Add($start)
$RemoteMachine = new-object System.Windows.Forms.TextB ox
$RemoteMachine.Location = new-object System.Drawing.Size(195,60 )
$RemoteMachine.Size = new-object System.Drawing.Size(100,20 )
$Form.Controls.Add($Remote Machine)
$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("Micro soft Sans Serif",12,[System.Drawing. FontStyle] ::Bold)
$label.Text = "Remote System"
$Form.Controls.Add($label)
$Form.Add_Shown({$Form.Act ivate()})
$Form.ShowDialog()
function pssession {
}
[System.Reflection.Assembl
[System.Reflection.Assembl
$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.Butto
$start.Location = new-object System.Drawing.Size(185,20
$start.Size = new-object System.Drawing.Size(120,30
$start.Text = "START"
$start.FlatAppearance.Mous
$start.ImageAlign = [System.Drawing.ContentAli
$start.FlatStyle = [System.Windows.Forms.Flat
$start.Add_Click({enter-ps
$Form.Controls.Add($start)
$RemoteMachine = new-object System.Windows.Forms.TextB
$RemoteMachine.Location = new-object System.Drawing.Size(195,60
$RemoteMachine.Size = new-object System.Drawing.Size(100,20
$Form.Controls.Add($Remote
$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("Micro
$label.Text = "Remote System"
$Form.Controls.Add($label)
$Form.Add_Shown({$Form.Act
$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.
$start.Add_Click({ $Form.Close(); enter-pssession $RemoteMachine.Text})
It will close the dialog, and then issue the command.
ASKER
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Marvolus help works exactly as I envisioned now