PowerShell中的特殊字符

问题描述 投票:1回答:1

我正在尝试使用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()

如何使这个版权角色和其他类似的特殊字符都能正常工作?

powershell scripting special-characters powershell-v2.0
1个回答
4
投票

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

  1. 您可以通过将Unicode表示形式转换为char来获取所需的任何字符。在这种情况下 [char]0x00A9
  2. 您为$SummaryPara.Range.Text分配了一个新值三次。所以,你每次都要覆盖以前的值,而不是连接('+'运算符),我认为这是你想要做的。
  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
© www.soinside.com 2019 - 2024. All rights reserved.