首页 > 解决方案 > 从 Powershell ISE 和 Windows 资源管理器运行脚本的不同行为

问题描述

当我从 Windows 资源管理器和 ISE 运行命令时,为什么我的命令表现不同?

我有一个来自 Test1.ps1 的简单命令(Test1 和 Test2 都在同一个文件夹中)

& ".\Test2.ps1"

当我从 Windows 资源管理器运行它时,会执行脚本 Test2.ps1。但是,当我在 ISE 中运行它时,它不起作用,并且出现以下错误:

& : Die Benennung ".\Test2.ps1" wurde nicht als Name eines Cmdlet、einer Funktion、einer Skriptdatei oder eines ausführbaren Programms erkannt。Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang。在 Zeile:1 Zeichen:3 +& ".\Test2.ps1"

  • CategoryInfo : ObjectNotFound: (.\Test2.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

对不起德国人:它基本上说:“.\Test2.ps1”没有作为 cmdlet、函数、脚本文件或已执行程序的名称找到。检查名称的输入或路径是否正确

标签: powershell

解决方案


请检查脚本窗格中显示的工作目录。ISE 通常从您的用户配置文件开始。要么先更改脚本的路径。

Set-Location -Path "C:\ScriptLocation\"

或者在调用脚本时使用完整路径:

& "C:\ScriptLocation\Test2.ps1"

当您从资源管理器运行它时,PowerShell 会在同一个文件夹中启动,这就是它能够找到脚本的原因。


推荐阅读