Question:
In my case, I needed to call a powershell script from a c or c++ code source, found few links which were pretty clumsy and not good with c++, I simply want a roadmap if its possible invoking a powershell script which lists directory contents from a code snippet written in c or c++
Answer:
C++ code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include #include #include #include #include using namespace std; void main() { string strPath = "d:\\callPowerShell.ps1"; //access function: //The function returns 0 if the file has the given mode. //The function returns –1 if the named file does not exist or does not have the given mode if(access(strPath.c_str(),0) == 0) { system("start powershell.exe Set-ExecutionPolicy RemoteSigned \n"); system("start powershell.exe d:\\callPowerShell.ps1"); system("cls"); } else { system("cls"); cout << "File is not exist"; system("pause"); } } |