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

[玩转系统] 如何使用Powershell写入主机

作者:精品下载站 日期:2024-12-14 18:43:57 浏览:16 分类:玩电脑

如何使用Powershell写入主机


PowerShell Write-Host cmdlet 用于将文本输出到用户的控制台。它通常用于显示脚本的进度、输出结果或向用户显示说明。

在本文中,我们将了解如何使用 Write-Host cmdlet、它附带的不同选项,并且我将向您展示几个示例。

PowerShell 写入主机 CmdLet

Write-Host cmdlet 实际上是旧版 Write-Information cmdlet 的包装器。在 PowerShell 5.0 之前,我们必须使用 Write-InformationWrite-Output cmdlet 将信息输出到控制台。旧的 cmdlet 没有提供很多功能,而 write-host 为我们提供了一些可以使用的样式选项:

-ForegroundColor

文字颜色

-BackgroundColor

文本的背景颜色

-NoNewline

输出后不会添加换行符

-Separator

在对象或数组中每个项目的输出之间添加分隔符

默认情况下,Write-Host cmdlet 将使用控制台的默认颜色(大多数情况下为白色)在控制台中的新行中输出字符串:

Write-Host "Welcome to the Lazy Script!"

[玩转系统] 如何使用Powershell写入主机

在 Write-Host 中使用颜色

颜色是吸引用户注意力或以非语言方式传达状态的好方法。例如,黄色文本通常表示警告,而红色文本很可能表示错误。在 PowerShell 中,我们可以使用 -foregroundcolor 参数为文本分配颜色,并使用 -backgroundcolor 为文本分配背景颜色。

[玩转系统] 如何使用Powershell写入主机

我们可以对这两个参数使用以下颜色:

  • 黑色的

  • 深蓝

  • 深绿色

  • 深青色

  • 深红

  • 深洋红色

  • 暗黄色

  • 灰色的

  • 深灰色

  • 蓝色的

  • 绿色的

  • 青色

  • 红色的

  • 品红

  • 黄色的

  • 白色的

格式化输出

默认情况下,Write-Host 输出的每个字符串都显示在新行上。在大多数情况下,这很好,但有时您不需要新行,或者输出之间可能不需要额外的新行。

我们首先看一下如何将结果追加到前面的 Write-Host 输出后面。为此,我们可以使用 -NoNewline 参数。添加后,下一个输出将附加到现有行:

Write-Host "Number of mailboxes: " -NoNewline

# Get all mailboxes function
$mailboxes = Get-AllMailboxes

Write-Host $mailboxes

# Result:
Number of mailboxes: 5

如您所见,值 $mailboxes 附加在“邮箱数量”行后面。

如果您想在输出之间创建一个额外的新空行,那么我们可以使用 `n 字符。这将在 write-host cmdlet 的输出后面添加额外的新行:

Write-Host "Getting all user mailboxes `n" -ForegroundColor Cyan
Write-Host "Shared mailboxes found `n `n" -ForegroundColor Yellow
Write-Host "Script finished" -ForegroundColor Green

[玩转系统] 如何使用Powershell写入主机

在 Write Host 中使用变量

值得一提的是,我们可以在 write-host cmdlet 的输出中使用变量。使用双引号 " " 时,可以在 write-host 字符串中包含一个简单的字符串变量。请记住,您不能在用单引号 ' ' 括起来的字符串内使用变量。此处的区别在于 PowerShell 仅处理用双引号括起来的字符串。

$user = "[email "
Write-Host "Getting mailbox of $user"

# Result
Getting mailbox of [email 

但是在使用对象时,您可能已经注意到无法输出对象的项目。例如,这个不会工作:

$user = [PSCustomObject]@{
    DisplayName = "Megan"
    Email = "[email "
}

Write-Host "Getting mailbox of $user.displayname"

要显示用户对象的显示名称,我们需要将其括在括号中,如下所示:

$user = [PSCustomObject]@{
    DisplayName = "Megan"
    Email = "[email "
}

Write-Host "Getting mailbox of $($user.DisplayName)"

# Result
Getting mailbox of Megan

使用分隔符

当你想将数组的结果输出到控制台时,默认情况下结果将显示在单行上。例如,我有一个包含一些水果的 CSV 文件。当我们导入 CSV 文件并输出结果时,它的格式将如下所示:

$example = import-csv -path C:\temp\example.csv -Header Fruit
Write-Host $example.fruit

# Result
Apple Banana Pear Kiwi Raspberry Melon

要在自己的行上显示每个水果或用字符分隔每个水果,我们可以使用 -Separator 参数:

# Show results on a new line
$example = import-csv -path C:\temp\example.csv -Header Fruit
Write-Host $example.fruit -Separator "`n"

# Result
Apple
Banana
Pear
Kiwi
Raspberry
Melon

# Separate results with a space and dash
$example = import-csv -path C:\temp\example.csv -Header Fruit
Write-Host $example.fruit -Separator " - "

# Result
Apple - Banana - Pear - Kiwi - Raspberry - Melon

编写主机格式示例

通过一些创造力,您可以在脚本中创建真正引人注目的不同横幅或警报。下面我收集了几个例子,希望对你有所启发。如果您还有其他好的例子,请在下面的评论中分享!

在警告消息上方和下方添加新行、在消息周围添加一些空格以及使用背景颜色确实可以使消息脱颖而出:

Write-Host "`n"
Write-Host "   RUNNING IN TEST MODE   "  -BackgroundColor Yellow -ForegroundColor Black
Write-Host "`n"

[玩转系统] 如何使用Powershell写入主机

脚本横幅总是有点难以正确处理。在下面的代码示例中,它看起来没有对齐,但是当在脚本中使用它时,您将看到一个完美的横幅。

Write-Host "`n"
Write-Host "                    -------------------------------------------------            " -ForegroundColor Cyan
Write-Host "                    |                                               |						" -ForegroundColor Cyan
Write-Host "                    |               Create new AD User              |						" -ForegroundColor Cyan
Write-Host "                    |                  Version 1.7                  |						" -ForegroundColor Cyan
Write-Host "                    |                                               |						" -ForegroundColor Cyan
Write-Host "                    |          Author R. Mens - LazyAdmin.nl        |						" -ForegroundColor Cyan
Write-Host "                    |                                               |						" -ForegroundColor Cyan
Write-Host "                    -------------------------------------------------            " -ForegroundColor Cyan
Write-Host "`n"

[玩转系统] 如何使用Powershell写入主机

我们还可以用前景色和一些 ASCII 艺术来做一些有趣的事情。下面的代码将每个字符的位置除以6。根据结果,它将为字符分配颜色。通过使用 -NoNewline 参数,我们可以附加结果:

$text = "
 _                      ___      _           _       
| |                    / _ \    | |         (_)      
| |     __ _ _____   _/ /_\ \ __| |_ __ ___  _ _ __  
| |    / _  |_  / | | |  _  |/ _  | '_   _ \| | '_ \ 
| |___| (_| |/ /| |_| | | | | (_| | | | | | | | | | |
\_____/\__,_/___|\__, \_| |_/\__,_|_| |_| |_|_|_| |_|
                  __/ |                              
                 |___/                               "


for ($i=0; $i -lt $text.length; $i++) {
    switch ($i % 6) {
        0 { $c = "white" }
        2 { $c = "green" }
        4 { $c = "blue" }
        default { $c = "cyan" }
    }
write-host $text[$i] -NoNewline -ForegroundColor $c
}

[玩转系统] 如何使用Powershell写入主机

总结

Write-Host cmdlet 是让脚本用户了解脚本进度的好方法。它可以向用户发出指令、显示结果或只是在脚本完成或遇到错误时通知用户。

充分利用前景色,因为它们确实有助于快速识别消息的含义。

希望您喜欢这篇文章,如果您有任何疑问,请在下面发表评论!

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

取消回复欢迎 发表评论:

关灯