.PS1 script does not work with PSHELL

Code:
c:\users\jlc\utils>ver

TCC  22.00.38 x64   Windows 7 [Version 6.1.7601]
I have a text file;
Code:
c:\users\jlc\utils>type test.txt
The
quick
brown
FOX
jumped
over
the
lazy
dog
I have a Powershell script;
Code:
c:\users\jlc\utils>type test.ps1
$a = Get-Content '.\test.txt'
$b = $a.Replace('FOX','Fox')
$b | out-file '.\test.txt'
type .\test.txt
When I run this Powershell script from TCC using PSHELL;
Code:
c:\users\jlc\utils>pshell test.ps1
PSHELL: System.Management.Automation.RuntimeException : You cannot call a method on a null-valued expression.
When I run this Powershell script from Powershell;
Code:
c:\users\jlc\utils>powershell -file test.ps1
The
quick
brown
Fox
jumped
over
the
lazy
dog
...it works as it should.

Why can PSHELL not run this script?

Joe
 
May 20, 2008
9,536
64
Syracuse, NY, USA
I can reproduce that but every time I successfully run the script with powershell.exe, the txtfile is rewritten in Unicode. Can that be avoided?
 
May 20, 2008
9,536
64
Syracuse, NY, USA
Thanks. You're original problem has to do with the start-up directory when using PSHELL. Below, $a is null because the file is not found in the current directory; "$a.Replace" generates the error.
Code:
$a = Get-Content '.\test.txt'
$b = $a.Replace('FOX','Fox')
I put a get-location in the script and hard-coded "v:\test.txt". It worked, and get-location shows why it wouldn't work using ".".

Code:
v:\> pshell test.ps1

Path
----
C:\Users\vefatica\Documents
The
quick
brown
Fox
jumped
over
the
lazy
dog
 
This one-liner replaces the .PS1, and works;
Code:
pshell /s "cd %_cwd; $a = Get-Content '.\test.txt'; $b = $a.Replace('FOX','Fox'); $b | out-file '.\test.txt'; type .\test.txt"
Still, would be nice if PSHELL would auto-magically recognize the current working directory from which it is executed.

Joe
 
May 20, 2008
9,536
64
Syracuse, NY, USA
Since the PowerShell interface used by PSHELL is lingering, it could make sense for its CWD to NOT follow TCC's CWD around (but I can only imagine rather contrived such scenarios).

If you do want PSHELL's CWD to always match TCC's CWD you might
Code:
alias pshell `*pshell %@pshell[set-location %_cwd]`
The use of @PSHELL adds nothing to the command line. That usually works, but very rarely gives the error below.
Code:
l:\> pshell /s get-location
PSHELL: System.Management.Automation.PSInvalidOperationException : The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.
That error might be a timing error due to two very fast calls to the interface. Rex might want to look into it.

Here's a slightly different alias (with which I haven't seen a problem) and a cute example of its use.

Code:
v:\> which pshell
pshell is an alias : %@pshell[set-location "%_cwd"] delay 0 & *PSHELL

v:\> global /q ( pshell /s get-location | grep : )
V:\
V:\22extract
V:\22extract\8FFDD1C
V:\22extract\8FFDD1C\32-bit
V:\22extract\8FFDD1C\styles
V:\22extract\a&b
V:\22extract\DataBackup
V:\22extract\DataBackup\Program Files (x86)
V:\22extract\DataBackup\Program Files (x86)\Hewlett-Packard
V:\22extract\DataBackup\Program Files (x86)\Hewlett-Packard\HP Setup
V:\22extractxx
V:\22extractxx\8FFDD1C
V:\22extractxx\8FFDD1C\32-bit
V:\22extractxx\8FFDD1C\styles
V:\22extractxx\a&b
V:\22extractxx\DataBackup
V:\22extractxx\DataBackup\Program Files (x86)
V:\22extractxx\DataBackup\Program Files (x86)\Hewlett-Packard
V:\22extractxx\DataBackup\Program Files (x86)\Hewlett-Packard\HP Setup
V:\a b
V:\cksum
V:\cksum\32
V:\cksum\64
V:\database
V:\dir1
V:\dir2
V:\extracted
V:\IPReg
V:\IPReg\country
V:\IPReg\country\ok
V:\IPReg\IPReg
V:\math
V:\tcchelp
V:\v16.55
V:\v16.55\110ED81
V:\v16.55\110ED81\Styles
V:\v8
 
May 20, 2008
9,536
64
Syracuse, NY, USA
Vince;
Code:
alias pshell `*pshell %@pshell[set-location %_cwd]`
...that alias is a keeper. Many thanks!

Joe
You might experiment with that. I had occasional errors, apparently from trying to access the PS interface twice in rapid succession.
Code:
v:\> alias pshell `*pshell %@pshell[set-location %_cwd]`

v:\> pshell /s get-location
PSHELL: System.Management.Automation.PSInvalidOperationException : The pipeline was not run because a pi
peline is already running. Pipelines cannot be run concurrently.

v:\> pshell /s get-location

Path
----
V:\
You might also want to quote %_cwd.