Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell对象执行脚本_Powershell_Powershell 3.0_Jobs_Start Job - Fatal编程技术网

使用powershell对象执行脚本

使用powershell对象执行脚本,powershell,powershell-3.0,jobs,start-job,Powershell,Powershell 3.0,Jobs,Start Job,使用的Powershell版本:3.0 各位好, 我希望尝试创建一个新的Powershell管道并在其中执行一个脚本,然后将它生成的输出获取到一个输出变量中,但是我无法从对象中(从执行的脚本)获取任何输出。这样我就不必管理$Error对象了,我打算用它来检测错误。下面是一个例子: $ps = [Powershell]::Create() $File = ".\Test2.ps1" $SortedParams = "-Name blah -Key foo" $RootDirectory = Ge

使用的Powershell版本:3.0

各位好,

我希望尝试创建一个新的Powershell管道并在其中执行一个脚本,然后将它生成的输出获取到一个输出变量中,但是我无法从对象中(从执行的脚本)获取任何输出。这样我就不必管理$Error对象了,我打算用它来检测错误。下面是一个例子:

$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
我应该提到,我正在尝试使用以下3种方法在执行的脚本中生成输出:

  • 写主机
  • 写入输出
  • Write Verbose(使用$ps.Streams.Verbose尝试获取输出,但什么都没有)

非常感谢您提供的任何建议或提示

您可以使用Invoke Expression cmdlet调用另一个脚本,并使用-OutVariable参数捕获其输出。我建议使用Out Null,这样数据就不会两次填充到控制台,请随意删除该管道命令。下面是一个例子:

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

Write-Host $data
这是我在上述示例中使用的示例脚本中的代码:

Param($ComputerName = ".")

Get-EventLog -ComputerName $ComputerName -Log application -EntryType Error | 
    Group-Object -Property source | 
    Sort-Object -Property Count -Descending | 
    Format-Table Count, Name -AutoSize

除非您觉得有特定的需要以您正在做的方式来做事情,否则您可能希望改用PowerShell后台作业

后台作业允许您在单独的PowerShell实例中运行PowerShell命令,然后将这些作业的输出收集到变量中(如果您希望这样做)

有关更多信息,请查看“关于工作”帮助主题

下面是一个简单的例子:

$job = Start-Job -ScriptBlock { "Hello World!" }
$ret = Receive-Job $job -Wait -AutoRemoveJob

# value of $ret will be "Hello World!"

检查错误流中的错误:

$ps.Streams.Error
这对我很有用:

$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
这显示了我的设置:

显示文件组织:

Command: 

tree.com /F /A $ExecutionDirectory

Output: 

C:\USERS\ANDY\DOCUMENTS\TEST3
    Test2.ps1
显示脚本内容:

Command:

cat "$ExecutionDirectory\Test2.ps1"

Output: 

param (
    $Name,
    $Key
)
$Name
$Key

我正在使用Invoke Expression,这是唯一适合我的解决方案:

test2.ps

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

$hola="Testing"
$hola
电话:


谢谢你的回复Colyn1337!但是,我有意避免从当前执行管道调用脚本,这样如果我选择手动执行此脚本,就不必清理$Error对象。这实际上是我以前使用的一个类似的实现。如果我找不到当前实现的解决方案,我会考虑使用它!告诉我更多你想要完成的事情。我可能能够进一步提供帮助:]基本上,我正在尝试完成3件事:-调用脚本-执行命令-将命令的输出传递回主脚本以进行日志记录我有意避免将输出直接推送到每个脚本中的日志文件。我试图在单个脚本中收集所有输出,然后将输出记录到日志文件中。我还应该提到,我不希望从当前管道执行此操作,因此我不必管理$Error变量清理。谢谢mikekol!我尝试使用这个逻辑,似乎返回的是命令本身,而不是脚本生成的输出。你能发布你的代码吗?这不是我预期会发生的情况,因此查看您正在执行的代码将更容易准确地理解正在发生的事情。代码如下:
$job=Start job-ScriptBlock{.\Test2.ps1-Name“blah”-Key“foo”“}$ret=Receive job$job-Wait-AutoRemoveJob Write host”输出:$ret
由于这里的格式问题,我不得不去掉双引号附近的两个“勾号”。现在开始。您刚刚获取该命令的原因是,它包含在双引号中,使PowerShell认为您只是将字符串文字放入管道中。如果将scriptblock更改为{.\Test2.ps1-Name“blah”-Key“foo”},这将使您更接近所需的内容。您可能还希望为要运行的脚本提供完全限定路径,因为作业可能不在当前PowerShell会话所在的文件夹中运行。太好了,谢谢!这个格式化问题稍微解决了逻辑问题,但当我运行它时,它仍然保留在同一个目录中:
$job=Start job-ScriptBlock{Set Location“$ExecutionDirectory”;Get Location}
C:\Test>powershell -ExecutionPolicy unrestricted -file test2.ps1
Return: Testing