I am trying to create a couple of files that I can save on my local desktop to launch PowerShell sessions.
The Windows Server 2008 and Windows Server 2012 are both Server Core installations.
Currently, I can open Powershell and type:
Enter-PSSession -computername Win2012SrvCore -credential administrator
Using this, I can connect and run commands and everything is great.
What I have tried to do is:
Create file called Win2012SrvCore1.ps1
with the following:
$passwd = convertto-securestring -AsPlainText -Force -String MYPASSWORD
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "administrator",$passwd
$session = new-pssession -computername Win2012SrvCore -credential $cred
Create file called Win2012SrvCore2.ps1
with the following:
PowerShell.exe -Command Enter-PSSession -computername Win2012SrvCore -credential administrator
Each ps1
file will launch and close quickly with some red text that I cannot read.
I have tried to add PAUSE
to each script but that doesn't seem to stop the window from closing.
What I would like to do is create scripts that I can double click and open to the powershell prompt, similar to a saved RDP session.
I have configured ps1 files to execute:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
Any help would be greatly appreciated.
add `-noexit'
PowerShell.exe -noexit -Command Enter-PSSession -computername Win2012SrvCore -credential administrator
Add one more line:
Import-PSSession $session
Then save the .PS1 file and create a shortcut to it as
powershell.exe -noexit -File "C:\PS.ps1"
.Try saving your commands as a script file and then have your shortcuts use the command-line:
powershell.exe -noExit <filename.ps1>
This will have your shortcuts run the specified script file and not exit powershell at the end of the scripts execution, so you can continue to use the window after the session is established.
For this to work you need to make sure the PowerShell execution policy is not Restricted, otherwise no script files can be executed
To check the current execution policy you can use
Get-ExecutionPolicy
and you can either useSet-ExecutionPolicy
to change the policy permanantly or add the-ExecutionPolcy
parameter to the powershell command-line to change it for a single session.More information about execution policies and their impact can be found using the
help about_Execution_Policies
command.