Using a powershell object to execute scripts

Powershell Version Used: 3.0

Hello everybody,

I am trying to create a new Powershell pipeline and execute the script inside it, and then get the output it produces into the output variable, but I cannot get any result created inside the object (from the executed script). The thing is that I do not need to manage the $ Error object, which I intend to use to detect errors. Here is an example below:

$ps = [Powershell]::Create() $File = ".\Test2.ps1" $SortedParams = "-Name blah -Key foo" $RootDirectory = Get-Location $ExecutionDirectory = "$RootDirectory\Test3" $ps.AddCommand("Set-Location").AddParameter("Path", "$ExecutionDirectory") write-host "COMMAND 1: " $ps.Commands.Commands.Item(0).CommandText $ps.AddScript("$File $SortedParams") write-host "COMMAND 2: " $ps.Commands.Commands.Item(1).CommandText $output = $ps.Invoke() write-host $output 

I should mention that I am trying to use the following 3 methods to create output in a script:

  • Write host
  • Write output
  • Write-Verbose (uses $ ps.Streams.Verbose to try to get the result, but nothing)

Any suggestions or tips you can give are greatly appreciated!

+4
source share
4 answers

If you don’t feel the need to do things the way you do them, you may need to use PowerShell background jobs instead.

Background jobs let you run PowerShell commands in a separate PowerShell instance, and then collect the output from these jobs into a variable (if that's what you want to do).

See the about_Jobs help topic for more information.

Here is an example:

 $job = Start-Job -ScriptBlock { "Hello World!" } $ret = Receive-Job $job -Wait -AutoRemoveJob # value of $ret will be "Hello World!" 
+2
source

You can invoke another script using the Invoke-Expression cmdlet and write its output using the -OutVariable parameter. I recommend using Out-Null so that the data does not populate the console twice, feel free to delete this command. Here is an example:

 Invoke-Expression -Command "c:\EventLog.ps1" -OutVariable $data | Out-Null Write-Host $data 

This is the code from the sample script that I used in the above example:

 Param($ComputerName = ".") Get-EventLog -ComputerName $ComputerName -Log application -EntryType Error | Group-Object -Property source | Sort-Object -Property Count -Descending | Format-Table Count, Name -AutoSize 
0
source

Check your error stream for errors:

 $ps.Streams.Error 

This worked for me:

 $ps = [Powershell]::Create() cd 'C:\Users\Andy\Documents' $File = ".\Test2.ps1" $SortedParams = "-Name blah -Key foo" $RootDirectory = Get-Location $ExecutionDirectory = "$RootDirectory\Test3" $ps.AddCommand("Set-Location").AddParameter("Path", "$ExecutionDirectory") | Out-Null $ps.AddScript("$File $SortedParams") | Out-Null $output = $ps.Invoke() write-host $output 

This shows my setup:

Show file organization:

 Command: tree.com /F /A $ExecutionDirectory Output: C:\USERS\ANDY\DOCUMENTS\TEST3 Test2.ps1 

Show script contents:

 Command: cat "$ExecutionDirectory\Test2.ps1" Output: param ( $Name, $Key ) $Name $Key 
0
source

I am using Invoke-Expression and this is the only solution that works for me:

test2.ps

 Invoke-Expression .\test1.ps1 | Tee-Object -Variable msg | Out-Null write-host "Return: $msg"</code> 

And test1.ps:

 $hola="Testing" $hola 

Call:

 C:\Test>powershell -ExecutionPolicy unrestricted -file test2.ps1 Return: Testing 
0
source

Source: https://habr.com/ru/post/1497479/


All Articles