Powershell脚本在粘贴到Powershell时运行,但在从快捷方式运行时不会运行

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

我正在尝试整合一个脚本,将几个excel文件转换为PDF。这是我第一次在Powershell中做这样的事情。 I found a link to one online that works

$path = Read-Host -Prompt 'Input Directory Path and Press Enter' 
$xlFixedFormat = “Microsoft.Office.Interop.Excel.xlFixedFormatType” -as [type] 
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse 
$objExcel = New-Object -ComObject excel.application 
$objExcel.visible = $false 
foreach($wb in $excelFiles) 
{ 
 $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdf”) 
 $workbook = $objExcel.workbooks.open($wb.fullname, 3) 
 $workbook.Saved = $true 
“saving $filepath” 
 $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath) 
 $objExcel.Workbooks.close() 
} 
$objExcel.Quit()

如果我将其复制并粘贴到Powershell中,程序将按预期运行。但是,当我尝试创建一个运行该程序的快捷方式时,我收到了几个错误(该文件保存为.ps1)。

这是我在设置快捷方式时所做的路径和参数:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -ExecutionPolicy Bypass -File C:\[File Path]

这是我收到的错误消息:

At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:8 char:62
+  $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdf ...
+                                                              ~
You must provide a value expression following the '+' operator.
At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:8 char:63
+ ... lepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdfâ€)
+                                                               ~~~~~~~~~~
Unexpected token '“.pdfâ€' in expression or statement.
At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:8 char:62
+  $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdf ...
+                                                              ~
Missing closing ')' in expression.
At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:7 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:8 char:73
+ ... lepath = Join-Path -Path $path -ChildPath ($wb.BaseName + “.pdfâ€)
+                                                                         ~
Unexpected token ')' in expression or statement.
At C:\Users\cbeals.ENVIROTECH\Documents\Test\ConvertExcelToPDF.ps1:14 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

任何想法为什么会失败?

powershell unicode utf-8 character-encoding codepages
1个回答
1
投票

澄清:

  • 在PowerShell中使用Unicode(非ASCII范围)引号(例如)是完全没问题的 - 请参阅底部部分。
  • 但是,为了在脚本文件中使用此类字符,这些文件必须使用Unicode字符编码,例如UTF-8或UTF-16LE(“Unicode”)。

您的问题是您的脚本文件保存为UTF-8而没有BOM,这导致Windows PowerShell(但不是PowerShell Core)误解它,因为它默认为“ANSI”编码,即与之关联的单字节遗留编码遗留系统区域设置(例如,美国和西欧的Windows-1252),PowerShell称之为Default

虽然用ASCII对应物替换Unicode引号可以解决当前的问题,但脚本中的任何其他非ASCII范围字符都会继续被误解。

  • 正确的解决方案是将文件重新保存为带有BOM的UTF-8。
  • 形成常规将所有PowerShell脚本(源代码)保存为带有BOM的UTF-8是一个好习惯,因为这样可以确保无论给定计算机的系统区域设置如何,它们都被解释为相同。 请参阅this related answer,其中显示了如何相应地配置Visual Studio代码。

要演示具体问题:

  • ,LEFT DOUBLE QUOTATION MARK(U+201C)Unicode字符,以UTF-8格式编码为3个字节:0xE2 0x80 0x9C。 您可以通过'“' | Format-Hex -Encoding Utf8的输出验证这一点(这里只有字节序列很重要;右边的打印字符在这种情况下不具代表性)。
  • 当Windows PowerShell将此序列作为“ANSI”-encode读取时,它会将每个字节视为一个字符,这就是为什么您在输出中看到单个的3个字符,即“。 您可以使用[Text.Encoding]::Default.GetString([byte[]] (0xE2, 0x80, 0x9C))验证这一点(来自PowerShell Core,使用[Text.Encoding]::GetEncoding([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage).GetString([byte[]] (0xE2, 0x80, 0x9C)))。

Interchangeable use of ASCII-range and Unicode quotation marks / dashes / whitespace in PowerShell:

在正确编码的输入文件中,PowerShell允许可互换地使用以下引号和标点字符;例如,"hi"”hi”甚至"hi„都是等价的。

注意:

  • 重要说明:以上描述了这些字符的可互换语法用法;如果您在标识符(您不应该使用)或字符串[1]中使用此类字符,则不会对它们进行相同的处理。
  • 以上是部分收集的from the source code on GitHubSpecialCharacters文件中的parserutils.cs类)。

[1]有一些例外:假设PowerShell的-eq运算符使用不变文化比较字符串而不是执行序数比较,则字符串比较中的空格字符变体可能会被视为相同,具体取决于主机平台;例如,"foo bar" -eq "foo`u{a0}bar"在macOS和Linux(但不是Windows!)上产生$true,因为常规的ASCII范围空间被认为等于那里的无中断空间(U+00A0)。

© www.soinside.com 2019 - 2024. All rights reserved.