Page 1 of 1

Run Powershell Rename script in Command Executor

Posted: Mon Oct 31, 2022 9:22 pm
by jukeboxjim
Hi,
I'm wondering if it's possible to run a PowerShell script from Command Executor for batch file renaming. My PowerShell command looks something like this:

Dir | Rename-Item –NewName {$_.name –replace "OldName","NewName"}

I realize this can be done with the folder delivery node, but if I wanted to keep the file name suffix: "OldName 089352" and only change the prefix: "NewName 089352", I don't know of any other way. I'm not really interested in a separate file renaming program.

Re: Run Powershell Rename script in Command Executor

Posted: Tue Nov 01, 2022 9:54 am
by emcodem
jukeboxjim wrote: Mon Oct 31, 2022 9:22 pm ... a separate file renaming program.
That is essentially what you do when going your powershell approach, no matter how you twist and turn it. My guess is that you actually just want a portable way to do your stuff that is e.g. saved within the workflow and will work when exporting/importing the workflow on another system without copying additional files "manually". If that is the goal, you could virtually do anything you want, you'd just need to find a way how to include (or fetch externally) the stuff with dependencies that you are going to use in the workflow.

Straight forward answer on "how to utilize powershell in a workflow":
There multiple ways to execute PS stuff, you can for example just use a command executor and do
cmd /C powershell -C "your powershell stuff"

But that will not be compatible to pipe symbol | ...at least i didnt yet find a quick way. Besides of course there are many other limitations and it is kind of hard to use.

Another example i often use is to write scripts to disk using the write text processor, save it in %s_job_work% directory and for powershell name it renamescript.ps1. When writing the script to disk using text file proc, you can nicely insert ffastrans variables into the script wherever you like, like this:
On Commandline:

Code: Select all

Dir "\\localhost\c$\temp" | Rename-Item –NewName {$_.name –replace "OldName","NewName"}  -ErrorAction 'Ignore'
In FFAStrans write text processor:

Code: Select all

Dir "%s_original_path%" | Rename-Item –NewName {$_.name –replace "OldName","NewName"}  -ErrorAction 'Ignore'
After write text processor, i insert cmd processor and execute the written script:
cmd /C powershell -executionpolicy unrestricted -file "%s_job_work%\renamescript.ps1"

Re: Run Powershell Rename script in Command Executor

Posted: Tue Nov 01, 2022 7:44 pm
by jukeboxjim
Thanks for your response! I made a workflow from what I think you are suggesting but haven't been able to get it to work. (attached)

Re: Run Powershell Rename script in Command Executor

Posted: Tue Nov 01, 2022 10:54 pm
by emcodem
I think it works as it is if you set the "Encoding" bottom right in the write text processor to "ansi" OR "utf8 BOM". currently it is set to utf8 (without byte order marker BOM), so powershell has no chance to guess the correct encoding of the script and it assumes ansi but actually it is utf8 more or less.

Re: Run Powershell Rename script in Command Executor

Posted: Wed Nov 02, 2022 11:12 am
by jukeboxjim
I switched it to ansi and it worked like a charm. Thank you so much! So, as I understand it, the Command executor script is telling it to run the PowerShell script in the text file processor.

Re: Run Powershell Rename script in Command Executor

Posted: Wed Nov 02, 2022 12:57 pm
by emcodem
jukeboxjim wrote: Wed Nov 02, 2022 11:12 am the Command executor script is telling it to run the PowerShell script in the text file processor.
It's very simple: Write text file writes a script to disk and command line processor executes a commandline to execute the powershell script file.
You could reproduce it manually by writing the script from notepad, save the file and execute the script via command line :D

Re: Run Powershell Rename script in Command Executor

Posted: Wed Nov 02, 2022 7:03 pm
by jukeboxjim
Cool beans :ugeek: