Powershell CTP v2 – Remoting: “Invoke-Expression” consideration…

Powershell Remoting is an excellent feature which can be exploit by everyone. But it has its limitation. At work, I need of building a solution that would automate a manual process for our IT staff. In this case, I had to create a Powershell script (psscript) that will allow me to copy a file in the user desktop for later retrieval without interrupting his work. So, I decided to use Powershell Remoting features using the CMDLET “Invoke-Expression” ran some with/without the “-runspace” switch.

Here’s what I found out :

1. If you have the need to passing strings parameters, no spaces are allowed. Exception – when using the “-runspace” parameter with “dir” commands. You might have a folder name with spaces “’My Documents”. In this case you need to add the single quote in the string, sample $dirName = “’My Documents’”.

2. You can’t use the “-credential” parameter past invoking the psscript on another computer. I mean, if within the script you is trying to copy a file to a network folder, the credential only work for executing the script.

3. When using the “-runspace” parameter make sure to use the “Remove-Runspace” CMDLET or the connection to the computer stays active.

See below:

Sample code for executing a PSscript remotely
" HelloPS.ps1 "

########################################
## HelloPS.ps1
## To test a string value with spaces
########################################

param($arg);
if ($arg -eq "Max Trinidad") {
Write-host -foreground green "*HelloPS* – Hello World!! My name is $arg, Its working!!"
} else {
write-host -foreground yellow "*HelloPS* – I’m missing my name again?? ** $arg **"}

## End of Script ##

Sample code for executing a PSscript remotely
" HelloPS2.ps1 "

########################################
## HelloPS2.ps1
## To test a string value with no spaces
########################################

param($arg);
if ($arg -eq "Max") {
Write-host -foreground green "*HelloPS2* – Hello World!! My name is $arg, Its working!!"
} else {
write-host -foreground yellow "*HelloPS2* – I’m missing my name again?? ** $arg **"}

## End of Script ##

String Sample #1 – Using the -runspace parameter showing losing of some string value when passing to a remote script.

String Sample #2 – showing how the filesystem CMDLET "Dir" handles a string value with spaces to a remote script.

String Sample #1 – Without the -runspace parameter showing losing of some string value when passing to a remote script.

Copy2Net Samples – Shows some limitation when trying to a "Copy" CMDLET.


So, as you can see, there some limitations in using this feature.  But, it doesn’t leave dead in the water.  You still can run script remotely as long as it contains no spaces, it must run local on the remote computer, and you can use the *-PSjob CMDLET to run in the background of the remote computer.  This coming Tuesday March 11th I will be speaking about "Powershell 2.0 CTP Remoting features". I will have my presentation and samples available later.

Advertisements

About maxt2posh

I’m from Puerto Rico, have been working with computers since 1979. Throughout many years, I have worked with SQL Server Technologies, provided support to Windows Servers/Client machines, Microsoft Virtualization Technologies, and build some Visual Studio solutions. I’m a Microsoft PowerShell MVP since 2009 and MVP SAPIEN Technologies since 2010. I speak in most of the SQLSaturday, IT Pro and .NET camps events around the Florida’s State. Also founder of the Florida PowerShell User Group which meeting every 3rd Tuesday evening of the month.
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s