gpt4 book ai didi

powershell - PowerShell 中的特殊字符

转载 作者:行者123 更新时间:2023-12-02 23:05:28 24 4
gpt4 key购买 nike

我正在尝试使用 PowerShell 并将版权字符输出到 Microsoft Word 文档中。例如,下面列出的代码是我正在尝试使用的代码,但它不起作用。

$SummaryPara.Range.Text = "© "
$SummaryPara.Range.Text = "Get-Date -Format yyyy"
$SummaryPara.Range.Text = " - Name of Org Here "
$SummaryPara.Range.InsertParagraphAfter()

我是否需要以某种方式使用 Alt + 0169 序列?

我不确定我做错了什么,因为下面的代码似乎有效:

$selection.TypeParagraph()
$selection.TypeText("© ")
$selection.TypeText((Get-Date -Format yyyy))
$selection.TypeText(" - Name of Org Here ")
$selection.TypeParagraph()

我怎样才能使版权字符和其他类似的特殊字符都起作用?

最佳答案

这里有一些问题。我将列出这些并解决每个问题:

  1. 通过将 Unicode 表示形式转换为 char,您可以获得所需的任何字符。在这种情况下

    [char]0x00A9
  2. 您为 $SummaryPara.Range.Text 分配了 3 次新值。因此,您每次都在覆盖以前的值,而不是连接('+' 运算符),我认为这正是您想要做的。

  3. 您正在尝试使用 cmdlet Get-Date ,但由于您引用了它,所以您最终会得到文字字符串“Get-Date -Format yyyy”,而不是 cmdlet 的结果。

综合起来,我想你想要这样的东西:

$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Add()
$SummaryPara = $doc.Content.Paragraphs.Add()
$SummaryPara.Range.Text = [char]0x00A9 + ($date = Get-Date -Format yyyy) + " - Name of Org Here "
$word.Visible = $true

关于powershell - PowerShell 中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com