众所周知,这Write-Host是邪恶的.In PowerShell 5,Write-Information被添加并被认为是替换Write-Host.
但是,真的,哪个更好?
Write-Host因为它不使用管道是邪恶的,所以输入消息不能被重用.
但是,Write-Host只是在控制台中展示一些东西呢?在什么情况下我们应该重用输入?
无论如何,如果我们真的想重用输入,为什么不写这样的东西:
$foo = "Some message to be reused like saving to a file"
Write-Host $foo
$foo | Out-File -Path "D:\foo.log"
Run Code Online (Sandbox Code Playgroud)
另一个缺点Write-Host是,Write-Host可以通过使用-ForegroundColor和指定消息在控制台中显示的颜色-BackgroundColor.
另一方面,通过使用Write-Information,输入消息可以通过No.6管道在任何我们想要的地方使用.并且不需要像我上面写的那样编写额外的代码.但是黑暗的一面是,如果我们想要将消息写入控制台并保存到文件中,我们必须这样做:
# Always set the $InformationPreference variable to "Continue"
$InformationPreference = "Continue";
# if we don't want something like this:
# ======= Example 1 =======
# File Foo.ps1
$InformationPreference = "Continue";
Write-Information "Some …Run Code Online (Sandbox Code Playgroud) 我很困惑-Passthru。
据我了解,该-Passthru参数用于将cmdlet的结果传递给管道,即使cmdlet本身不产生任何结果。
我尝试先编写这些代码:(我尝试不使用Restart-WebAppPoolcmdlet 来回收IIS应用程序池)
Stop-WebAppPool -Name Foo
Start-WebAppPool -Name Foo
Run Code Online (Sandbox Code Playgroud)
如我所料,停止IIS ApplicationPool需要花费时间,并且由于PowerShell不等待cmdlet完成其工作,因此将Stop-WebAppPool停止应用程序池并返回。由于应用程序池尚未准备好开始,因此Start-WebAppPool将调用并引发错误。
因此,我将代码更改为以下代码:
Stop-WebAppPool -Name Foo
Start-Sleep -Seconds SomeValueNeedToGetMeasuredAndChanged
Start-WebAppPool -Name Foo
Run Code Online (Sandbox Code Playgroud)
它可以工作,但不太灵活。
然后,我-Passthru将这两个cmdlet 添加到其中,它变为:
Stop-WebAppPool -Name Foo -Passthru
Start-WebAppPool -Name Foo -Passthru
Run Code Online (Sandbox Code Playgroud)
一切正常,没有错误。
就我而言,我认为:
在真正停止/启动应用程序池之前,Stop / Start-WebAppPool cmdlet不会产生任何结果。由于-Passthru需要将某些内容传递到管道,因此PowerShell等待cmdlet产生其结果,在这种情况下,将停止/启动应用程序池以执行下一个。
我在-Passthru或上都找不到任何有用的手册Stop-WebAppPool,因此在这种情况下可以这样做吗?