Running powershell script on asp.net site -


i trying run powershell script , have output asp.net site. have made work simple script command in script

get-service | out-string

and output onto site expected

but when use script want info doesn't output can tell runs (or trys run) because when site hits code invokes script hangs 10 seconds.

the script trying run is

$user = "user" $token = "token"  $base64authinfo = [convert]::tobase64string([text.encoding]::ascii.getbytes(("{0}:{1}" -f $user,$token)))  $result = invoke-webrequest -method -uri 'https://site.vsrm.visualstudio.com/defaultcollection/product/_apis/release/releases?definitionid=1&api-version=3.0-preview.2&$expand=environments' -contenttype "application/json" -headers @{authorization=("basic {0}" -f $base64authinfo)}  $releasearr = $result.content | convertfrom-json [system.collections.arraylist]$enviromentname = @() [system.collections.arraylist]$latestrelease = @()   foreach($env in $releasearr.value[0].environments) {     $enviromentname.add($env.name) | out-null }   foreach($releasevalue in $releasearr.value) {     for($i = 0; $i -lt $enviromentname.count; $i++)     {         if($latestrelease[$i] -eq $null)         {             foreach($release in $releasevalue.environments)             {                 if($release.name -eq $enviromentname[$i] -and $release.status -eq "succeeded")                 {                     $latestrelease.add($releasevalue.name) | out-null                 }             }         }     } }  for($i = 0; $i -lt $enviromentname.count; $i++) {     write-host $enviromentname[$i] " : " $latestrelease[$i] } 

i know script runs , outputs, there code in script cause not output properly.

the code in asp.net site using call script is

resultbox.text = string.empty;          // initialize powershell engine         var shell = powershell.create();          // add script powershell object         shell.commands.addscript(@"c:\users\user\desktop\script.ps1");          // execute script         var results = shell.invoke();          // display results, baseobject converted string         // note : use |out-string console-like output         if (results.count > 0)         {             // use string builder ton create our result text             var builder = new stringbuilder();              foreach (var psobject in results)             {                 // convert base object string , append string builder.                 // add \r\n line breaks                 builder.append(psobject.baseobject.tostring() + "\r\n");             }              // encode string in html (prevent security issue 'dangerous' caracters < >             resultbox.text = server.htmlencode(builder.tostring());         } 

change "write-host" "write-output." write-host outputs interactive consoles.

you can see in action:

make new powershell file , add write-host statement it:

[nick@nick-lt temp]$ new-item -type file -path .\example.ps1 -force [nick@nick-lt temp]$ set-content .\example.ps1 "write-host 'hello world'" 

then try , set variable result of script:

[nick@nick-lt temp]$ $what = .\example.ps1 hello world [nick@nick-lt temp]$ $what [nick@nick-lt temp]$ 

hello world shows when script executes variable empty.

now change write-output:

[nick@nick-lt temp]$ set-content .\example.ps1 "write-output 'hello world'" [nick@nick-lt temp]$ $what = .\example.ps1 [nick@nick-lt temp]$ $what hello world 

the variable contains supposed now.

one of cardinal rules of powershell not use write-host except in script run interactively. .net needs results in output stream not host stream.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -