Powershell call from script

Jul 7, 2020
2
0
Hi I am trying to call this line:

Code:
FOR /f %%i IN ('"powershell (Get-Date).AddMonths(-1).ToString(\"MM\")"') DO SET lastmonth=%%i

from a script and I get an this error:

Code:
Unknown command "powershell (Get-Date).AddMonths(-1).ToString(\"MM\")"

It works in an batch file but not from TCC.

How can I make it work in TCC?

Thanks for your help
 
May 20, 2008
11,400
99
Syracuse, NY, USA
In short, quote get rid of some double quotes. I prefer DO.

Code:
v:\> type pstest.btm

for /F %%i in ('powershell (Get-Date).AddMonths(-1).ToString(\"MM\")') echo %i

do i in /P powershell (Get-Date).AddMonths(-1).ToString(\"MM\") ( echo %i )

v:\> pstest.btm
06
06

v:\>
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
This is a simple TCC way to do that:
Code:
function lastmonth=`%@if[%((%_month-1)) == 0,12,%((%_month-1))]`
echo %@lastmonth[]
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
I don't notice any difference speed-wise between the %@if[] version and the modulo math version.

And if you want the leading zero - e.g. 06 vs. 6 - you can do %@format[02,%@lastmonth[]]
 
May 20, 2008
11,400
99
Syracuse, NY, USA
How are you timing? Depending on how I time it, I see from no difference to a significant difference (MOD being faster than enhances @IF, which is faster than original @IF. These tests are very repeatable.

Here's the no difference one.

Code:
v:\> do f in /L lastmonth1 lastmonth2 lastmonth3 ( function %f & timer /q & do i=1 to 10000 ( echo %@%f[] > NUL ) & timer & echo
.)
%(((%_month+10) mod 12 + 1))
Timer 1 off: 12:39:56  Elapsed: 0:00:03.136

%@if[%_month == 1,12,%((%_month-1))]
Timer 1 off: 12:39:59  Elapsed: 0:00:03.133

%@if[%((%_month-1)) == 0,12,%((%_month-1))]
Timer 1 off: 12:40:02  Elapsed: 0:00:03.135

Here's one showing significant (~12%/~12%) differences.

Code:
v:\> timer /q & do i=1 to 10000 ( echo %@lastmonth1[] > nul ) & timer
Timer 1 off: 12:45:25  Elapsed: 0:00:03.998

v:\> timer /q & do i=1 to 10000 ( echo %@lastmonth2[] > nul ) & timer
Timer 1 off: 12:45:33  Elapsed: 0:00:04.436

v:\> timer /q & do i=1 to 10000 ( echo %@lastmonth3[] > nul ) & timer
Timer 1 off: 12:45:42  Elapsed: 0:00:04.844

Here's another with (perhaps) minimized overhead (NOOP literally does nothing). It shows them in the same order with the same ~12%/~12% differences.

Code:
v:\> timer /q & do i=1 to 10000 ( noop %@lastmonth1[] ) & timer
Timer 1 off: 12:46:51  Elapsed: 0:00:02.530

v:\> timer /q & do i=1 to 10000 ( noop %@lastmonth2[] ) & timer
Timer 1 off: 12:46:57  Elapsed: 0:00:02.857

v:\> timer /q & do i=1 to 10000 ( noop %@lastmonth3[] ) & timer
Timer 1 off: 12:47:08  Elapsed: 0:00:03.203
 
Code:
     _x64: 1
   _admin: 1
_elevated: 1

TCC  26.01.40 x64   Windows 10 [Version 10.0.18362.900]

I would do it this way;
Code:
e:\utils>powershell -command "& {(Get-Date).AddMonths(-1).ToString('MM')}"
06

...or...
Code:
e:\utils>pshell /s "(Get-Date).AddMonths(-1).ToString('MM')"
06

..or..
Code:
e:\utils>echo %@pshell[(Get-Date).AddMonths(-1).ToString('MM')]
06

Joe
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Code:
Code:
v:\> do f in /L lastmonth1 lastmonth2 lastmonth3 ( function %f & timer /q & do i=1 to 10000 ( echo %@%f[] > NUL ) & timer & echo
.)
%(((%_month+10) mod 12 + 1))
Timer 1 off: 12:39:56  Elapsed: 0:00:03.136

%@if[%_month == 1,12,%((%_month-1))]
Timer 1 off: 12:39:59  Elapsed: 0:00:03.133

%@if[%((%_month-1)) == 0,12,%((%_month-1))]
Timer 1 off: 12:40:02  Elapsed: 0:00:03.135

Actually, that one showed no differences because it didn't work correctly.

Code:
v:\> function lastmonth1
%(((%_month+10) mod 12 + 1))

v:\> set f=lastmonth1

v:\> echo %@%f[]
echolastmonth1[]

Any ideas how to put the name of a UDF in a variable (and use it)?
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
I don't get consistent results running the same function over and over let alone the variants. But the timings are so subtle anyway.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Hmmm! This would work ...

Code:
v:\> set f=lastmonth1

v:\> function %f
%(((%_month+10) mod 12 + 1))

v:\> echo %%@%f[]
%@lastmonth1[]

... if it were parsed one more time.

Code:
v:\> echo %@upper[%%@%f[]]
6
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Scott, I asked about Terminal elsewhere and go no response. Please tell me ...

Is it intrusive in any way? Does it become the default way console apps are run? Can it be ignored? It it easy to start TCC in Terminal? [and whatever other questions I should have asked]

I got this from the Microsoft Store: "Microsoft.WindowsTerminal_1.0.1401.0_8wekyb3d8bbwe.msixbundle". Just the name of it tells me it's going to have a goofy installation (like many store apps). Is there another way?
 
Pardon my two cents.

Windows Terminal does not become the default way console apps are run.

You can still use the classic conhost.exe, but Windows Terminal is better.

Windows Terminal uses (where it can) the GPU to make the display much faster.

The only thing I don't like about Windows Terminal is having to modify the settings.json file by hand.

Here are the additions that I had to make to settings.json to add TCC to Windows Terminal.
1594154625472.png


I used the TCC UUID command to generate the guid for TCC.
Everything has to be entered by hand into the settings.json file.

Here is the documentation for Windows Terminal, so that you can see all of the other settings you can add to the settings.json file.

Mind you, this is not something you have to do all the time, just tweak it to your liking, then leave it alone, just like the TCMD.INI file.

You cannot detach the individual Windows from Windows Terminal.

Install it (Windows Terminal, NOT Windows Terminal Preview) from the Microsoft Store. It is quite customizable.

Joe
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Thanks, Joe ... got it running. So far, I'm not impressed. To start with:

Code:
v:\> ffind /s c:\settings.json

      0 files

I did find but I wonder ... why hide a config file?
 
Jul 7, 2020
2
0
In short, quote get rid of some double quotes. I prefer DO.

Code:
v:\> type pstest.btm

for /F %%i in ('powershell (Get-Date).AddMonths(-1).ToString(\"MM\")') echo %i

do i in /P powershell (Get-Date).AddMonths(-1).ToString(\"MM\") ( echo %i )

v:\> pstest.btm
06
06

v:\>

Thank you all very much for your help :smile:
 
Similar threads
Thread starter Title Forum Replies Date
F Powershell call fails with "Scripting Integrator 2016 (PowerShell)" license error Support 9
CWBillow TCC and TCMD in Powershell Support 6
Joe Caverly PowerShell=No Support 1
A Problem with functions @int @decimal and identifying Powershell as a shell. Support 12
Joe Caverly PowerShell Core Support 1
CWBillow PowerShell tab as Admin Support 2
CWBillow TCMD PowerShell Tab elevated Support 6
CWBillow Take Command tab as PowerShell Support 3
vefatica Powershell ... insert vs overstrike Support 2
CWBillow Powershell tab in TCMD? Support 2
Joe Caverly Fixed Displaying Profiles from TCC vs. PowerShell Support 2
Joe Caverly How to use Powershell Profile with PSHELL Support 14
CWBillow PowerShell Tabs in TCMD Support 11
I v16 Spawning a PowerShell tab Support 3
vefatica "which" in PowerShell invokes TCC? Support 3
B Powershell scripts not opening in notepad-TC main suspect Support 2
electrotype Powershell in Take Command - Customizing colors? Support 4
C Attaching PowerShell changes its colors and makes it hard to read Support 15
jdanielp Cannot step into and/or call another .bat file when debugging .bat file Support 2
S Debugger Bat file call history Support 9
M Call History popup-window? Support 0
nickles Call a complex function Support 13
rconn News Take Command v19 Call for Features Support 0
cgunhouse Problem with CALL and Directory Aliases in V17 Support 9
Vasilich How to? debugging files that use CALL command - possible?? Support 3
noahcoad Option to always CALL? Support 4
M @ExecStr vs. Call Support 4
O CALL SYSTEM from Fortran Support 4
rconn Last call for v12 feature requests Support 59
rconn Call for v11 feature requests Support 35
p.f.moore Running another batch file without CALL Support 7
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
Joe Caverly .PS1 script does not work with PSHELL Support 11
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

Similar threads