Logon to Windows via PowerShell Session


The number of Remote Desktop Process session on windows machine is restricted. Windows client allows one session and Windows server allows up to two sessions. If they allow more than two sessions like SSH, it is convinient. Actually, instead of RDP session, we can establish other multiple connections by using PowerShell session.

EnterPSSession.ps1
$target = "ServerName"
Set-Item WSMan:\localhost\client\trustedhosts -value $target -force

$user = "User"
$password = "Password"
$pass = convertto-securestring -string $password -asplaintext -force
$credential = new-object system.management.automation.pscredential($user,$pass)

Enter-PSSession -computername $target -credential $credential -p 5985


It is not neccessary to establish a interactive session if you just execute a command. It is also possible to execute command on remote server as follows.

InvokeCommand.ps1
#exec command on win rm
#open ps session
$session = new-pssession -computername $server -credential $credential

#exec command on remort server
invoke-command -session $session -scriptblock { echo "Hello!!" }

#close ps session
$session | remove-pssession


Off course, file transfering between local machine and remote server is available.

FileTransfer.ps1
#file transfer on winrm
#open ps session
$session = new-pssession -computername $server -credential $credential

#send to server
#in case of file
copy-item -path "c:\localdir\file.txt" -destination "c:\serverdir\" -tosession $session
#in case of directory
copy-item -path "c:\localdir\dir\" -destination "c:\serverdir\" -tosession $session -recurse

#send to local macihne
#in case of file
copy-item -path "c:\serverdir\file.txt" -destination "c:\localdir\" -fromsession $session
#in case of direcroty
copy-item -path "c:\serverdir\dir\" -destination "c:\localdir\" -fromsession $session -recurce

#close ps session
$session | remove-pssession


Note that setup for WinRM on remort server is mandatory. HTTP connection port is changeable.

WinRMServerSideSetup.ps1
#server side
set-executionpolicy remotesigned
enable-psremoting

#change HTTP connection port (default 5985)
set-item wsman:\localhost\listner\listener*\port -value 5985
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
Objective
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
Link
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)

Archives