15

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.

slayernoah
  • 1,570
  • 2
  • 12
  • 19
Anthony Fornito
  • 9,526
  • 1
  • 33
  • 122
  • What happens if you manually type this in the ISE line by line, command by command? Do you still get an error? – Colyn1337 Nov 25 '14 at 20:34
  • I would open up Powershell ISE (Integrated Scripting Environemt) and run the script. This will display the output and allow you to continue troubleshooting. – BRNDR Nov 25 '14 at 21:00

3 Answers3

12

add `-noexit'

PowerShell.exe -noexit -Command Enter-PSSession -computername Win2012SrvCore -credential administrator

Craig
  • 585
  • 2
  • 12
10
$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

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".

TheCleaner
  • 32,352
  • 26
  • 126
  • 188
9

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 use Set-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.

Crippledsmurf
  • 241
  • 2
  • 11