当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

作者:精品下载站 日期:2024-12-14 05:02:55 浏览:16 分类:玩电脑

PowerShell 打印 |在 PowerShell 中打印输出的不同方式


[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

PowerShell 打印简介

将输出写入控制台或文件是任何语言的重要要求。输出通常可以是单行、多行或表达式或表达式的结果。打印值还可用于调试或作为错误处理机制的一部分。在 PowerShell 中打印输出有多种方法。 PowerShell 中的一些打印方式包括 Write-Output、Write-Host、Write-Verbose、Write-Warning 和 Write-Error。

在 PowerShell 中打印输出的不同方式

下面给出了在 PowerShell 中打印输出的不同方式:

1. 写输出

打印输出的第一种方法是使用 Write-Output cmdlet。此 cmdlet 用于将管道中的对象传递给后续命令。如果命令是最后一个,则最终对象将写入控制台。要传递错误对象,请使用 Write-Error cmdlet。

语法:

Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]

参数:

  • -InputObject:这表示应通过管道传递的对象。它可以是表达式、对象变量或表达式。该参数的数据类型是PSObject[]。它的默认值是无。它接受管道输入,但不接受通配符。
  • -NoEnumerate:默认情况下,写入输出 cmdlet 会枚举输出。为了避免这种情况,使用此参数。该参数的数据类型是Switch。无是默认值。它不接受管道输入以及通配符,这也是不允许的。

示例:

代码:

Write-Host "Demo of write host cmdlet" -ForegroundColor Green
$test= Get-Process
Write-Host "Printing a variable" -ForegroundColor Green
Write-Output $test
Write-Host "Passing output to another cmdlet through pipeline" -ForegroundColor Green
Write-Output "sample output" | Get-Member
Write-Host "Example of enumerate and no enumerate" -ForegroundColor Green
Write-Output 10,20,30 | Measure-Object
Write-Output 10,20,30 -NoEnumerate | Measure-Object

输出:

[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

2. 写主机

此 cmdlet 的主要用途是以不同颜色显示输出。这与 read-host cmdlet 一起工作,后者检索用户的输入。此 cmdlet 使用 ToString() 方法生成输出并将其写入控制台。我们可以分别使用前景和背景参数指定输出的文本颜色和背景颜色。为了分隔输出中的各个对象,分隔符参数作为 cmdlert 的一部分出现。有两个变量;与此参数关联的 $informationpreference 和 $Informationaction 参数,但它们不影响输出。

参数:

  • -BackgroundColor:这表示背景的颜色。没有默认颜色。一些值是灰色、绿色、蓝色、红色、白色、深绿色、深蓝色和磁红色。该参数的数据类型是控制台颜色。它不接受管道输入,也不允许使用通配符。
  • -前景色:这表示文本的颜色。没有默认颜色。一些值是灰色、绿色、蓝色、红色、白色、深绿色、深蓝色和磁红色。该参数的数据类型是控制台颜色。它不接受管道输入,也不允许使用通配符。
  • -NoNewLine:这表示所有输出都写在同一行上,并且输出之间不插入换行符。参数的数据类型是开关。默认值为无。它不接受管道输入,也不允许使用通配符。
  • -对象:这表示要作为输出写入的对象。该参数的数据类型是对象。此参数的别名是 Msg 和 message。这是一个强制参数。默认值为无。它接受管道输入,但不允许使用通配符。
  • -分隔符:这表示要在对象之间插入的分隔符。该参数的数据类型是对象。默认值为无。它不接受管道输入,并且不允许使用通配符。

示例:

代码:

Write-Host "Demo of write-host"
Write-Host "printing in same line example" -NoNewline
Write-Host "test of second line"
Write-Host "example with separator"
Write-Host (20,49,69,80,10,128) -Separator ", +4= "
Write-Host "Example of background colours"
Write-Host (21,41,62,81,110,121) -Separator ", ->> " -ForegroundColor Blue -BackgroundColor White
Write-Host "Iam vignesh, from chennai" -ForegroundColor DarkGreen -BackgroundColor white
Write-Host "Preventing from printing"
Write-Host "this wont be displayed" -InformationAction Ignore
Write-Host "it wont be available on screen" 6>$null

输出:

[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

3. 写调试

这用于从脚本或命令在控制台中打印调试消息。默认情况下,不会显示消息,但可以在需要时使用 $debugPreference 变量显示消息。

语法:

Write-Debug [-Message] <String> [<CommonParameters>]

参数:

  • -消息:这表示需要显示的调试消息。该参数的数据类型是字符串。这是一个强制参数。该参数的别名是 msg。 None 是其默认值。它接受管道输入,但不允许使用通配符。

示例:

代码:

Write-Host "Demo of write-debug"
Write-Debug "This wont be printed"
Write-Debug "my name is vignesh"
Write-Host "Changing the value of debug preference variable" -ForegroundColor Green
Write-Host "Current value is" -ForegroundColor Green
$DebugPreference
Write-Debug "wont print this on console"
$DebugPreference = "Continue"
Write-Debug "now this will be displayed"

输出:

[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

4. 详细写入

Write-Verbose cmdlet 将文本写入 PowerShell 中的详细消息流。通常,详细消息流用于传递有关命令处理的更全面的信息。默认情况下,不会显示详细消息流,但可以通过更改 $VerbosePreference 变量的值或在任何命令中使用 Verbose 通用参数来显示。

语法:

Write-Verbose [-Message] <String> [<CommonParameters>]

参数:

  • -消息:这表示需要显示的消息。这是一个强制参数。该参数的数据类型是字符串。别名是 msg。默认值为无。它接受管道输入,但不允许使用通配符。

示例:

代码:

Write-Host "Example of verbose command"
Write-Verbose -Message "Searching the error in Event viewer."
Write-Verbose -Message "Searching the error in Event viewer." -Verbose

输出:

[玩转系统] PowerShell 打印 |在 PowerShell 中打印输出的不同方式

结论 - PowerShell 打印

因此,本文详细介绍了 PowerShell 中的打印。它详细解释了打印输出的各种方式以及适当的示例。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯