Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 foreach对象中似乎不起作用-并行_Powershell_Parallel Processing_Output_Powershell 7.0_Foreach Object - Fatal编程技术网

写入信息在powershell foreach对象中似乎不起作用-并行

写入信息在powershell foreach对象中似乎不起作用-并行,powershell,parallel-processing,output,powershell-7.0,foreach-object,Powershell,Parallel Processing,Output,Powershell 7.0,Foreach Object,我是powershell的新手,正在学习它。我有一些C#方面的经验。我试图使用foreach对象并行选项,但无法使所有Write-*函数正常工作 function writeTest { 1..1 | ForEach-Object -Parallel { Write-Host "host" Write-Output "Output" Write-Information

我是powershell的新手,正在学习它。我有一些C#方面的经验。我试图使用foreach对象并行选项,但无法使所有Write-*函数正常工作

function writeTest {
        1..1 | ForEach-Object -Parallel {
            Write-Host "host"
            Write-Output "Output"
            Write-Information "information" -InformationAction Continue
            Write-Verbose "verbose"
            Write-Warning "Warning"
            Write-Error "error"       
        }
}
函数调用类似:writeTest-verbose

产出:

host
Output
WARNING: Warning
Write-Error: error
我的问题是,为什么写冗长和写信息不显示任何内容

请原谅任何无知。

您在
ForEach Object-Parallel
/
Start ThreadJob
中看到一个bug,至少出现在PowerShell Core 7.0中:

您的
写入信息
输出应该显示出来,因为您已使用
-InformationAction Continue
将其打开;虽然您的
Write Verbose
输出预期不显示,因为您没有使用
-Verbose
打开它,但是如果您使用了
-Verbose
,由于该错误,它也不会显示

解决方法是在调用
ForEach Object-Parallel
之前,设置首选项变量
$InformationPreference
,以打开信息流(见下文)

另外,您的代码存在一个概念问题

  • 您正在将
    -Verbose
    公共参数传递给
    编写测试
    函数,但此函数未声明为高级函数(它需要
    [CmdletBinding()]
    属性位于
    参数(…)
    块上方和/或至少一个参数具有
    [parameter(…)]
    属性-请参阅),因此,该参数将不起作用

  • 即使它确实生效,这意味着PowerShell将其转换为一个函数本地
    $VerbosePreference
    变量,其值为
    'Continue'
    ,但
    ForEach Object-Parallel
    块将看不到该变量,因为需要
    $using:
    作用域来引用调用者作用域中的变量值;看

把这一切放在一起

function Write-Test {

  # Mark the function as an advanced one, so that it accepts 
  # common parameters such as -Verbose
  [CmdletBinding()]
  param()
  
  # WORKAROUND: Turn the information stream on 
  #             via its *preference variable* AS WELL:
  $InformationPreference = 'Continue'
  
  1..1 | ForEach-Object -Parallel {
    Write-Host "host"
    Write-Output "Output"
    Write-Information "information" -InformationAction Continue               
    Write-Verbose "verbose" -Verbose:($using:VerbosePreference -eq 'Continue')
    Write-Warning "Warning"
    Write-Error "error"       
  }

}
  
Write-Test -Verbose
请注意传递给
-Verbose
开关的表达式,它必须与开关名称用
-
-Verbose:($using:verboseproference-eq'Continue')分开。
-实际上,只有在函数主线程中的
$verboseproference
值(
$using:
)时,才打开详细输出设置为
'Continue'
,当从外部将
-Verbose
传递给高级函数时,会发生这种情况(如果调用方的作用域中将
$VerbosePreference
设置为
'Continue'
,由于PowerShell的动态作用域,也会发生这种情况;请参阅)


有关PowerShell输出流的一般信息

  • 默认情况下,
    Write Verbose
    Write Information
    都是静默的,就像
    Write Debug
    一样

  • 您可以通过以下两种方式之一显示其输出:

    • 每个命令,通过一个公共参数:将
      -Verbose
      添加到
      Write Verbose
      调用和
      InformationAction Continue
      添加到
      Write Information
      调用(如您所做)

    • 范围广泛,通过首选项变量:设置
      $VerbosePreference='Continue'
      $InformationPreference='Continue'
      ——但警告是,在(其他)模块中运行的函数和在不同线程或进程中运行的代码默认情况下都看不到这些变量:

      • 在模块情况下,必须显式使用公共参数或在全局范围内设置首选项变量,这是不可取的;这一问题在本文中讨论
      • 在另一个线程/进程情况下,需要
        $using:
        作用域,如上所示

请参见和

默认情况下不显示这两个流。看看各自的首选项$vars,类似于
Get Variable-Name*preference*
。。。[咧嘴笑]真棒的回答!!!非常感谢。我有一个更大的模块,在那里出现了错误,我试着使它尽可能小。你对冗长的评论也是我的理解。实际功能(显然不是写测试)与您描述的一样。我也明白我不应该使用Write-Host,我应该遵循动词-名词的格式——我已经在实际代码中尽了最大的努力。非常感谢你!!!我在这方面花了好几个小时,但作为一个新手,我觉得我把事情搞砸了。很高兴听到这有帮助,@tcocunk;我的荣幸。不幸的是,所有这些都是棘手的业务,即使对于经验丰富的用户来说也是如此。