4

I want to be able to run like 30 lines of PowerShell script from my c++ program. I've heard it's a terrible idea, I don't care. I still would like to know how.

I just want to code directly in the C++ program, i do not want to externally call the PowerShell script. Is there any way to do this? If it's impossible, just say no.

For example

void runPScode() {

//command to tell compiler the following is PowerShell code
//a bunch of PowerShell code

}

Thanks!

I've looked for commands to do this and have read several 'similar' questions.

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
Bulbasaur
  • 303
  • 3
  • 19

4 Answers4

16

This just for completeness' sake:


PowerShell has an API - see System.Management.Automation.PowerShell. The API is managed (i. e. .NET-based). It's possible to build a mixed mode C++ application and invoke said API from the managed part.

Place the following into a separate C++ file:

#include "stdafx.h"
#include <vcclr.h>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Management.Automation.dll>

using namespace System;
using namespace System::Management::Automation;

void RunPowerShell(LPCWSTR s)
{
    PowerShell::Create()->AddScript(gcnew String(s))->Invoke();
}

In Project Properties, under VC++ Directories, add C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0 to Reference Directories (your path may vary).

Set the following compiler options for that file only:

  • Common Language RunTime Support (/clr)
  • Debug information format - Program Database (/Zi)
  • Enable C++ exceptions - No
  • Basic runtime checks - Default
  • Precompiled header - Not Using Precompiled Headers

You need /clr for calling .NET from C++, but /clr is incompatible with a bunch of other C++ options. If you miss something, the compiler error messages will let you know.

Declare void RunPowerShell(LPCWSTR) as a regular external function in the unmanaged parts of the project, invoke as needed.


That said, whatever your Powershell does, C++/Win32 probably can do too. That said, some one-line features of Powershell (e. g. the remoting) would translate into hundreds of lines of C++.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
4
//command to tell compiler the following is PowerShell code

No! There's no such command to tell the compiler, and

//a bunch of PowerShell code ...

being executed inline.

You can achieve this using the CreateProcess() function choosing your shell, and provide it with the appropriate code to execute.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

You have two options: using system or CreateProcess. The system documentation is found at:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019

Using this method you pass a string command. An example, as shown in the documentation:

system( "type crt_system.txt" );

CreateProcess documentation is found at:

https://learn.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa

Using this command is a little trickier and I wouldn't recommend it for simple commands.

For additional information please see: how can we use a batch file in c++?

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • Welcome to Stack Overflow! Please provide the specific method and explanation in your answer and link to the website as a reference, rather than just linking out to an external resource. – spicy.dll Jun 07 '19 at 18:14
  • This should be a comment, not an answer. If it is a duplicate question, [vote to close](//stackoverflow.com/help/privileges/close-questions) as such and/or leave a comment once you [earn](//meta.stackoverflow.com/q/146472) enough [reputation](//stackoverflow.com/help/whats-reputation). If not, *tailor the answer to this specific question*. – double-beep Jun 07 '19 at 18:15
  • those answers are for the command prompt, i'm using powershell, are the commands the same? – Bulbasaur Jun 07 '19 at 18:20
  • He has more than two options. There's also [the exec family](https://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html). – Jesper Juhl Jun 07 '19 at 18:46
  • Can the exec family be used for command prompt? I thought it was used specifically for bash commands – MichaelBeneamatoBruno Jun 07 '19 at 19:12
0

How I do it at work... is just with a shortcut on the desktop... what's even more tricky? Getting XAML to pin on the taskbar...

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -WindowStyle hidden -File "C:\Users\USER01\Documents\scripts\runClassModule.ps1"

and just set the shortcut with this target.

So that in c++ ... just call out the shortcut...

system("c:\\users\\USER01\Desktop\\yourshortcut\\shortcut.lnk") ; 

or just the code itself

system("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -WindowStyle hidden -File C:\\Users\\USER01\\Documents\\myApp\\modulesScripts\\runClassModule.ps1");

This opens a variety of win32 apps and forms I use for automatic motion.

Going all the way and adding a password while examining some of the codes used by the shell....

 system("C:\\Windows\\System32\\runas.exe  /profile  /user:MYDOMAIN\\USER01\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -WindowStyle hidden -File C:\\Users\\USER01\\Documents\\myApp\\modules\\runClassModule.ps1\""); 
Dana M
  • 1
  • 4