【问题标题】:PowerShell throwing errors on blank lines, not recognizing integers or commandsPowerShell 在空行上抛出错误,无法识别整数或命令
【发布时间】:2021-08-11 23:10:13
【问题描述】:

我正在开发一个系统管理脚本,该脚本从两个大型/复杂模块中提取 - 一个用于错误记录/报告,另一个用作服务器 API 的库。我遇到了一些非常奇怪的错误,这些错误让我认为这些库中的某些东西使环境处于不良状态。即使在我重新启动 ISE/主机并删除所有复杂代码后,它们似乎仍然存在。这是我目前的测试:

set-psdebug -trace 2​

pause

pause​
# Initialize Paths and Filenames
 $Network_Share = "\\EC2AMAZ-R2G8HFL\Backup"

 pause

输出:

PS D:\Scripts\Scripts_7-6> D:\Scripts\Scripts_7-6\test8-11.ps1
Set-PSDebug : Cannot bind parameter 'Trace'. Cannot convert value "2​" to type "System.Int32". Error: "Input string was not in a correct format."
At D:\Scripts\Scripts_7-6\test8-11.ps1:1 char:20
+ set-psdebug -trace 2​
+                    ~~
    + CategoryInfo          : InvalidArgument: (:) [Set-PSDebug], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SetPSDebugCommand
 
Press Enter to continue...: 
pause​ : The term 'pause​' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\Scripts\Scripts_7-6\test8-11.ps1:5 char:1
+ pause​
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (pause​:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
​ : The term '​' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\Scripts\Scripts_7-6\test8-11.ps1:8 char:1
+ ​
+ ~
    + CategoryInfo          : ObjectNotFound: (​:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Press Enter to continue...: 

PS D:\Scripts\Scripts_7-6> 

如果我没看错,它会抛出关于“2”不是 Int32 兼容值、第二个“暂停”不是可识别命令(尽管第一个和第三个按预期工作!)的错误,最后是第 8 行的空行被解释为命令(尽管第 2、4 和 10 行的空行不是)。

有些东西真的让我的环境陷入了循环,我什至不知道从哪里开始解决这个问题。谁能指点我一些可以帮助我弄清楚发生了什么的资源?

编辑:为 Set-PSDebug 添加语法

PS D:\Scripts\Scripts_7-6>> Help Set-PSDebug

NAME
    Set-PSDebug
    
SYNOPSIS
    Turns script debugging features on and off, sets the trace level, and toggles strict mode.
    
    
SYNTAX
    Set-PSDebug [-Off] [<CommonParameters>]
    
    Set-PSDebug [-Step] [-Strict] [-Trace <Int32>] [<CommonParameters>]
    
    
DESCRIPTION
    The Set-PSDebug cmdlet turns script debugging features on and off, sets the trace level, and toggles strict mode.
    
    When the Trace parameter is set to 1, each line of script is traced as it is executed. When the parameter is set to 2, variable assignments, function calls, and script calls are also traced. If the Step parameter is specified, you are prompted before each line of the script is executed.
    

RELATED LINKS
    Online Version: http://go.microsoft.com/fwlink/p/?linkid=289612
    Debug-Process 
    Set-PSBreakpoint 
    Set-StrictMode 
    Write-Debug 
    about_Debuggers 

REMARKS
    To see the examples, type: "get-help Set-PSDebug -examples".
    For more information, type: "get-help Set-PSDebug -detailed".
    For technical information, type: "get-help Set-PSDebug -full".
    For online help, type: "get-help Set-PSDebug -online"

【问题讨论】:

  • 运行 Help Set-PSDebug 并发布函数/cmdlet 的语法。
  • @AbrahamZinala 将结果添加到上面的帖子中。我使用的语法是从 MS 脚本演示中复制而来的,看起来很合理。不知道为什么“2”不是整数。

标签: powershell


【解决方案1】:

您的命令在2 后面包含一个不可见字符,这就是2 未被识别为整数的原因,同样的问题同样会影响第二个pause 语句的结尾行。

有问题的不可见字符是零宽度空间,U+200B。

This answer 包含函数Debug-String,可以帮助您检测此类问题。

一个简单的修复方法是删除将光标放在受影响行的末尾并按Backspace;如有疑问,请删除并重新输入每个受影响的行。

【讨论】:

  • 成功了。我以为我已经检查了控制字符,这是 Notepad++ 第一次没有为我显示字符。非常感谢专业知识。
  • 很高兴听到这个消息,@user1953403;我的荣幸。
猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多