Running a Powershell script from c# -


i'm trying run powershell script c# code, i'm having (maybe environmental) issues:

on machine try run it, following occur:

  1. powershell starts , loads admin
  2. powershell window closes (apparently) without error

notes:

  • the script works. when run ise, runs without errors.
  • if right click script , choose run powershell, execution policy error though don't change in script.

set-executionpolicy : windows powershell updated execution policy successfully, setting overridden policy defined @ more specific scope. due override, shell retain current effective execution policy of remotesigned. type "get-executionpolicy -list" view execution policy settings. more information please see "get-help set-executionpolicy". @ line:1 char:46 + if((get-executionpolicy ) -ne 'allsigned') { set-executionpolicy -scope process ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : permissiondenied: (:) [set-executionpolicy], securityexception + fullyqualifiederrorid : executionpolicyoverride,microsoft.powershell.commands.setexecutionpolicycommand

  • get-executionpolicy -list

         scope                executionpolicy      -----                ---------------  machinepolicy              unrestricted     userpolicy                 undefined        process                    bypass    currentuser              unrestricted   localmachine              unrestricted 
  • i believe environmental because:

    • it ran fine few days ago
    • it runs fine on other computers

this code use invoke script:

if (file.exists("start.ps1")) {     string strcmdtext = path.combine(directory.getcurrentdirectory(), "start.ps1");      var process = system.diagnostics.process.start(@"c:\windows\system32\windowspowershell\v1.0\powershell.exe ", strcmdtext);     process.waitforexit(); } 

the script irrelevant, have changed simple

write-host "hello" $d=read-host 

and have same issue.

the problem in path of script. had spaces on particular machine , had not handled that.

the window closed fast see error setting

process.startinfo.redirectstandardoutput = true; 

helped me catch it.

the execution policy had nothing error.

to fix changed path in c# code explained here: executing powershell script in cmd.exe location "illegal characters in path"

complete code:

if (file.exists("start.ps1"))             {                 file.getattributes("start.ps1");                 string strcmdtext =   path.combine(directory.getcurrentdirectory(), "start.ps1");                 var process = new process();                 process.startinfo.useshellexecute = false;                 process.startinfo.redirectstandardoutput = true;                 process.startinfo.filename = @"c:\windows\system32\windowspowershell\v1.0\powershell.exe";                 process.startinfo.arguments = "\"&'"+strcmdtext+"'\"";                  process.start();                 string s = process.standardoutput.readtoend();                 process.waitforexit();                  using (streamwriter outfile = new streamwriter("standardoutput.txt", true))                 {                     outfile.write(s);                 }              } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -