.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
11,400
99
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
11,400
99
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
 
May 20, 2008
11,400
99
Syracuse, NY, USA
More simply:
Code:
v:\> pshell /s get-location

Path
----
C:\Users\vefatica\Documents
I wonder of Rex has any control over that?
 
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
11,400
99
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
11,400
99
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.
 
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly SCRIPT and GetObject Support 3
vefatica SCRIPT ... TCC disappears Support 2
T How can I send a simple parameter to a script, in tc 25? Support 2
vefatica PSHELL: How pass arguments to script? Support 15
EDerH Powershell call from script Support 24
MickeyF running program from script and (not) returning to script Support 12
T Fixed Echo randomly activated in script Support 4
fpefpe How to? script error Support 1
fpefpe How to? script error Support 0
vefatica @SCRIPT bug persists Support 4
D Catching the result of an evaluated script expression Support 0
Joe Caverly Non-conforming @Script Support 6
M The Matrix: a useless script ... Support 5
fpefpe How to? LUA script Support 0
T Change language in script Support 4
Steve Pitts Documentation Script errors invoking help Support 15
R How to? Launch and execute commands in tcmd.exe through MSBuild Script. Support 3
Joe Caverly @SCRIPT does not like all comments Support 2
Mordachai Help! How to set parameters for script I'm debugging?! Support 6
fpefpe How to? Script support Support 8
JohnQSmith "SCRIPT" Support 7
rconn Script kiddies on the attack Support 4
krischik Stop Script after Syntax Error. Support 6
H Ruby - Running External Ruby script crashes TCC Support 11
Joe Caverly SCRIPT Command Support 2
T Obfuscating passwords in script Support 14
Stefano Piccardi IDE.exe start toobar button fails when % in script path Support 0
lassevk exit(1) in a python script exits console, by design or bug? Support 1
R break script on syntex error Support 7
K iftp script copy deletes target files Support 17
R script problem with mapped network drive Support 1
D Parsing an array variable to an array does not work Support 5
J Take command does not start on Windows 10 Support 3
J ffind does not find files Support 4
Joe Caverly PAUSE does not erase countdown timer Support 0
D ParentDirectory does not update directory history Support 6
D Documentation Help "Startup" page does not describe global lists Support 0
Charles Dye Does HASH's /E option do anything? Support 1
fishman@panix.com First TCC instantiation after reboot does not work. Support 17
D Fixed @VERSION does not increment pre-fixed version numbers. Support 1
vefatica Why does @WSLPATH double the leading '/'? Support 7
D What does the file "..\Everything\Run history.csv" store? Support 5
Peter Murschall WAD rem does not work correctly Support 8
vefatica Does @SYMLINK work? Support 6
R Tree on FAT32 does not sort by name by default. Support 6
A Tcc does not recognize "bcdedit" Support 20
MickeyF Why does this batch file give me 'unknown command "else"' error? Support 17
M Ctrl-C when using command line history does nothing Support 2
vefatica JOBS /K ... how does it work? Support 5
Joe Caverly Why does TCCRT return VIEW as an internal command? Support 6

Similar threads