asp.net - How do I execute child ps1's when calling the parent ps1 from .NET -


i have asp.net application wishing call powershell script results. i've done digging , come lot of dated information relating ps1/2 nothing ps4 upwards.

i have powershell script presently completes operations , outputs arraylist powershell console. powershell script calls powershell script:

.\get-azuresubscription.ps1  $environments = new-object system.collections.arraylist $resourcegroups = get-azurermresourcegroup  foreach($thisresourcegroup in $resourcegroups) {     $resourcegroupvms = get-azurermvm -resourcegroupname $thisresourcegroup.resourcegroupname      $environment = new-object -typename psobject     $environment | add-member -membertype noteproperty -name resourcegroupname -value $thisresourcegroup.resourcegroupname     $environment | add-member -membertype noteproperty -name id -value $thisresourcegroup.resourceid      $machines = new-object system.collections.arraylist      foreach($thismachine in $resourcegroupvms)     {         $machine = new-object -typename psobject         $machine | add-member -membertype noteproperty -name name -value $thismachine.name         $machine | add-member -membertype noteproperty -name resourcegroupname -value $thisresourcegroup.resourcegroupname           $status = get-azurermvm -resourcegroupname $thisresourcegroup.resourcegroupname -name $thismachine.name -status | `                          select-object -expandproperty statuses | `                          where-object { $_.code.startswith("powerstate") } | `                          select-object -expandproperty code          $regex = [regex] '\/(.*?)$'         $machine | add-member -membertype noteproperty -name status -value $regex.match($status).groups[1].value           $machines.add($machine) > $null;     }      $environmentstatuses = $machines | select-object resourcegroupname -expandproperty status | group-object resourcegroupname, status      $environment | add-member -membertype noteproperty -name statuses -value $environmentstatuses     $environments.add($environment) > $null; }  $environments 

i have following .net code attempts execute outer script having content of ps1 script passed in string:

public collection<psobject> runscript(string script, dictionary<string, string> parameters = null) {     // validate parameters     if (string.isnullorempty(script)) { throw new argumentnullexception(nameof(script)); }     runspaceconfiguration runspaceconfiguration = runspaceconfiguration.create();      using (runspace runspace = runspacefactory.createrunspace(runspaceconfiguration))     {         runspace.open();          using (runspaceinvoke invoker = new runspaceinvoke(runspace))         {             invoker.invoke("set-executionpolicy unrestricted");              pipeline pipeline = runspace.createpipeline();              command scriptcommand = new command(script, true);             parameters?.select(kvp => new commandparameter(kvp.key, kvp.value))                 .foreach(scriptcommand.parameters.add);              pipeline.commands.add(scriptcommand);              var result = pipeline.invoke();              return result;         }     } } 

the issue if call ps1 .net, fails complaining the term '.\get-azuresubscription.ps1' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again. if execute ps1 powershell, executes without issue.

if copy , paste content of get-azuresubscription.ps1 ps1 directly called .net instead of linking 2 scripts, passed .net successfully.

there's no guarantee working directory of .net application same script invoke within it.

you can use trick figure out full path invoking script (replace first line this):

$azuresubscriptionscript = join-path $psscriptroot "get-azureassistantsubscription.ps1" & $azuresubscriptionscript 

$psscriptroot automatic variable holds path of current/invoking script's location on disk value


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -