《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记—CHAPTER 19 Input and output

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_41104353/article/details/82347413

19.2 Read-Host

Read-Host 用来收集用户输入

PS C:\> read-host "Enter a computer name"
Enter a computer name: SERVER-R2
SERVER-R2

我们可以将输入的值保存到变量中:

PS C:\> $computername = read-host "Enter a computer name"
Enter a computer name: SERVER-R2

使用ISE则会弹出对话框。
当然,我们也可以在cli中弹出对话框:
Creating a graphical input box in Windows PowerShell

19.3 Write-Host

我们可以使用 Write-Host 来展示输出。可以使用 -foregroundColor-backgroundColor 参数改变输出的前景色和背景色。

PS C:\> write-host "COLORFUL!" -fore yellow -back magenta
COLORFUL!

Write-Host runs in the pipeline like any other cmdlet, but it doesn’t place anything into the pipeline. Instead, it writes directly to the hosting application’s screen.

19.4 Write-Output

Write-Output 将对象送入pipeline中,最终是pipeline本身展示这些对象。
因此,由于不是 Write-Output 本身对对象进行展示,就不允许我们指定输出的颜色。

19.5 Other ways to write

Alternative output cmdlets

如果我们想要使用上述的一些Write cmdlet,需要先允许使用他们。比如,设置$VerbosePreference="Continue"来允许 Write-Verbose

猜你喜欢

转载自blog.csdn.net/sinat_41104353/article/details/82347413
今日推荐