Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Powershell call from script

Jul
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
 
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:\>
 
This is a simple TCC way to do that:
Code:
function lastmonth=`%@if[%((%_month-1)) == 0,12,%((%_month-1))]`
echo %@lastmonth[]
 
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[]]
 
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
 
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)?
 
I don't get consistent results running the same function over and over let alone the variants. But the timings are so subtle anyway.
 
I got this to work:
Code:
do f in /l %%@lastmonth1 %%@lastmonth2 (echo %f[])

Yeah, I did exactly that too. But that's not the name of the function. You can't even echo %f, let alone use FUNCTION on it to see what it does.
 
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
 
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
 
Kayla Cinnamon is the Program Manager from Microsoft who is responsible for the Windows Terminal project.

You can read her posts realtime.

Joe
 
The Windows Terminal repository contains the source code for Windows Terminal, so that you can build it yourself on your system, making changes to the source code as you want to.

Joe
 
Found this link on GitHub for the error that you are getting.

Did you install Windows Terminal from the store?

Joe
 
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?
 
Not hidden on my system.

From Windows terminal, click settings from the menu, or press CTRL+, to open the settings.json file.

Joe
 
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

Back
Top