Running Multiple Remote Commands With Enter-PSSession

by Typeo at 2013-03-18 10:10:03

Hi,

I am trying to setup a script to automatically start a PSSession with a server and run some commands on. However, I seem to be running into a wall. All my commands when I run the script, want to run locally rather than remotely.

Here is my script:

# Lets do this!

$pw = convertto-securestring -AsPlainText -Force -String ‘Password’
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\Name",$pw

Enter-PSSession -ComputerName Computer1 -Credential $cred

#Imports the custom LocalAccounts Module from C:/Users/Username/Documents/WindowsPowerShell/Modules on the remote server
Import-Module LocalAccounts

#Sets the Username Variable
$Username = Read-Host ‘Enter Preferred Username.’

if ((Get-LocalUser -all | Where-Object {$.name -eq $Username} | Select-Object -ExpandProperty Name) -eq ($Username) -or (!$Username))
{
#Do, Until loop that checks whether the the username is unique and is not Null
Do
{
$Username = Read-Host ‘That username already exists, please enter a new one!’
}

Until
((Get-LocalUser -all | Where-Object {$
.name -eq $Username} | Select-Object -ExpandProperty Name) -ne ($Username) -and ($Username))
}

#sets the Password varible
$Password = Read-Host ‘Enter the Username Password.’ -AsSecureString


#Creates the new user with the specified Username and Password variables
New-LocalUser -Name $Username -Password $password

Read-Host ‘New User Has Been Created! Press Enter to Continue!’

Exit-PSSession


Now, I thought since I ran a Enter-PSSession that all the following commands would run via the remotely connected machine. This does not seem to be the case. The commands are trying to run locally.

I know I can use the Invoke-Command, however, I would need to do that for every command and it would make the Enter-PSSession pointless. Is that the only way I can accomplish this though?
by mjolinor at 2013-03-18 10:17:28
You need to use invoke-command, but you don’t have to send every command individually. Put all the commands together in a scriptblock, and send it the scripblock.
by MasterOfTheHat at 2013-03-18 10:59:20
Interesting… I’d never tried to use Enter-PSSession in a script, so I was surprised to see it didn’t work. It makes sense when you remember that Enter-PSSession was designed for interactive use, though.

You could also use the -FilePath parameter to send the entire script to the remote machine, correct? (sans the Enter-PSSession part of it…) And it looks like you could actually create a new remote session using New-PSSession and pass that to Invoke-Command via the -Session parameter, though I’m not sure why you would…
by Typeo at 2013-03-18 11:43:38
Thank you both for the help!

@mjolinor - Thanks for the suggestion… I am fairly new with the Invoke-Command Cmdlet, but I am sure I can figure out how to accomplish putting it all in a script block. Ill def give that a try next.

@MasterOfTheHat - Ya, I figured the Enter-PSSession would be the best / easiest way to do what I was wanting done. Doesn’t seem to work the way I had envisioned it though. Ill have to look for the reason why as I am now curious as to what it limiting it.
by Typeo at 2013-03-18 12:32:53
Just wanted to give you a heads up that placing it all in a script block did indeed fix my issue.

Thanks a bunch for the help!

Great for creates the new (local) user .

Is it possible add the user o another user (domain user) to Administrators group ?

Thx.