1

We use a lot of BAT\CMD scripts for our day-to-day operations and due to their limitations, at times we sometimes have to rely VBS\PS scripts, called from those, then continue the batch script afterwards.

We have a security limitation in which those scripts can only be executed locally. I vaguely remember, once using a method of opening a PowerShell session from within the batch file, where native PS cmdlets can be used. I have since lost that code and was hoping that someone could share its syntax.

The intention is to pass information into and out of the PS session so that the batch file can respond according to the PS output?

Compo
  • 36,585
  • 5
  • 27
  • 39
Nana
  • 21
  • 4
  • Are you thinking of Powershell Remoting? https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands – MisterSmith Sep 30 '22 at 20:20
  • 1
    This an hybrid code Batch and Powershell example is just to show you how to put a multiline comment block with powershell and how to execute Batch section and powershell section : [Hybrid Code Example Batch-Powershell](https://stackoverflow.com/a/73894215/3080770) – Hackoo Oct 01 '22 at 07:37
  • Thanks, @Hackoo, yes I was looking for the same, HYBRID. Thanks a lot... – Nana Oct 01 '22 at 19:40
  • Hi @MisterSmith, I wanted to use multiple PowerShell commands in the same BAT file and I don't want to keep calling powershell.exe every time I'm executing those commands. As Hackoo pointed out, I need a PowerShell hybrid mode within the BAT file. – Nana Oct 01 '22 at 19:44
  • Can you flip your solution around - have a powershell script that can execute batch files? (at its simplest `cmd /c somebat.bat arg1 arg2 arg3 etc` from a `.ps1` file). That would allow you to run multiple posh commands without starting multiple posh sessions, and you can open a remoting session, copy files to the destination and execute them? Another options might be `powershelluniversal` - you can create a simple web service in posh and use `curl http://localthost/...` to execute POSH from bat. Also worth noting powershell is much more powerful than bat files - maybe its time to upgrade?:-) – MisterSmith Oct 01 '22 at 20:21
  • @MisterSmith I duly agree sir. I started my career with PowerShell scripting and came across BAT later. I'm stuck up with BAT at the moment due to security restrictions and I don't want to copy multiple files while executing the operation, that's the reason I was looking for an alternative and I found that with others' help. Thanks again for your suggestion, soon we will update our template to PS1 :) – Nana Oct 03 '22 at 19:44

1 Answers1

-1

You can execute a powershell script from a batch file. Below is the command you will put in your batch.

Powershell.exe -executionpolicy remotesigned -File "PathoftheFile\Filename.ps1"
Dheeraj Mutha
  • 79
  • 1
  • 1
  • 8