【问题标题】:Writing my first powershell script: commands are not recognized in the script编写我的第一个 powershell 脚本:脚本中无法识别命令
【发布时间】:2015-06-25 23:59:53
【问题描述】:

重命名项目 A B

在 shell 中键入时,如果本地目录中有文件 A,则重命名有效。

当我写了一个单行脚本,把它放在一个文件夹里,导出这个文件夹的路径,去另一个文件夹,里面有一个文件A,然后调用这个脚本,会发生这样的事情:

'rename-item' 未被识别为内部或外部命令,
可运行的程序或批处理文件。

我错过了什么?

【问题讨论】:

  • 我怀疑您正在尝试从常规命令提示符 (cmd.exe) 而不是从 powershell 命令提示符 (powershell.exe) 调用 powershell 脚本,因为这正是您会遇到的错误进入常规命令提示符。
  • @EBGreen,我正在从 powershell 调用文件。但该文件名为 rename.bat。可能这是我的问题。
  • 这绝对是你的问题。将其重命名为 .ps1
  • @EBGreen,耶,更进一步。能否请您写一个简短的答案,解释为什么会出现奇怪的错误消息,以便很好地完成这个问题?

标签: powershell


【解决方案1】:

当您运行批处理文件时,您正在调用一个包含 PowerShell 命令的批处理文件。命令提示符通过 Powershell 引导其输出,这是上述错误。 Command.exe 无法运行 powershell cmdlet,所以当它到达 Rename-Item 时,它不是一个好的内部或外部命令、可运行的程序或批处理文件。

当您 Ping www.google.com 时会调用 Ping.exe,例如什么是好的命令或可执行文件。

Powershell 可以从 command.exe 运行命令,反之则不行。

如果您打开 cmd 提示符并尝试运行 powershell 命令或 cmdlet,您会看到相同的结果。:

H:\>rename-item 'rename-item' is not recognized as an internal or external command, 
operable program or batch file.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2015-09-10
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
相关资源
最近更新 更多