powergui 中的点源失败

发布于 2024-11-29 15:19:52 字数 631 浏览 0 评论 0原文

我正在尝试在 PowerGui 3.0 中对脚本文件进行点源,但我得到的只是;


术语“.\PowerShell.Common.ps1”未被识别为 cmdlet、函数、脚本文件或可操作程序。检查拼写 名称,或者如果包含路径,请验证该路径是否为 正确并重试。在 D:\TFS\SharePoint\Dev\Deploy\AutoSPInstaller\SP2010\AutoSPInstaller\AutoSPInstallerFunctionsCustom.ps1:6 字符:31 + .\PowerShell.Common.ps1 <<<<< + 类别信息:未找到对象: (.\PowerShell.Common.ps1:字符串) [], CommandNotFoundException + FullQualifiedErrorId:CommandNotFoundException


并且 powerGui 随后不在所述文件中提供我的脚本函数 - 在父脚本的上下文相关列表中。

文件“PowerShell.Common.ps1”与 AutoSPInstallerFunctionsCustom.ps1 位于同一目录中 感谢您的协助

I'im trying to dot-source a script file in PowerGui 3.0 , but all i get is ;


The term '.\PowerShell.Common.ps1' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spel
ling of the name, or if a path was included, verify that the path is
correct and try again. At
D:\TFS\SharePoint\Dev\Deploy\AutoSPInstaller\SP2010\AutoSPInstaller\AutoSPInstallerFunctionsCustom.ps1:6
char:31
+ .\PowerShell.Common.ps1 <<<<
+ CategoryInfo : ObjectNotFound:
(.\PowerShell.Common.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


And powerGui subsequently does not offer my script function within said file - in the context sensitive list in the parent script.

the file "PowerShell.Common.ps1" is in the same directory as AutoSPInstallerFunctionsCustom.ps1
Thank you for your assistance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱了心跳 2024-12-06 15:19:52

要从 PowerGUI 的命令行对文件进行点源化,请确保当前工作目录位于脚本的目录中。您可以通过在 PowerGUI 的命令行中键入 $PWD 来检查这一点。

要从脚本引用另一个脚本,您可以执行以下操作:

# Get the current script's directory
$MyDir = Split-Path $MyInvocation.MyCommand.Definition

# Dot-source the external script by using the current script's directory
. "$MyDir\ScriptName.ps1"

获取脚本的目录可确保即使当前工作目录与脚本的目录不同,您也能够引用相对于脚本位置的文件。

To dot-source the file from PowerGUI's command line, make sure that your current working directory is at the script's directory. You can check this by typing $PWD at PowerGUI's command line.

To reference another script from a script you would do this:

# Get the current script's directory
$MyDir = Split-Path $MyInvocation.MyCommand.Definition

# Dot-source the external script by using the current script's directory
. "$MyDir\ScriptName.ps1"

Getting the script's directory ensures that even if your current working directory is not the same as the script's directory, you will be able to reference files relative to the script's location.

可遇━不可求 2024-12-06 15:19:52

@Rynant 指出问题是您需要引用脚本的目录而不是当前目录,这当然是正确的。然而,值得注意的是,他的代码解决方案仅部分正确;事实上,它是否有效取决于你在哪里调用它!

一个更强大的解决方案是这样的:

function Get-ScriptDirectory
{
    Split-Path $script:MyInvocation.MyCommand.Path
}

碰巧,我刚刚写了一篇详细的讨论,分析了在另一个 SO 中正确获取脚本目录的这一点问题。我不会重复我冗长的答案(包括测试车辆和结果矩阵),而是提供 此链接

@Rynant is certainly correct in pointing out that the problem is you need to reference the script's directory rather than your current directory. However, it is important to note that his code solution is only partially correct; in fact, whether it works depends on where you call it!

A more robust solution is this:

function Get-ScriptDirectory
{
    Split-Path $script:MyInvocation.MyCommand.Path
}

As it happens, I just wrote a detailed discussion analyzing this very point of correctly getting the script directory in another SO question. Rather than repeat my lengthy answer (complete with test vehicle and results matrix) I will provide this link.

沐歌 2024-12-06 15:19:52

当您从 PowerGUI 中浏览到正在处理的脚本时,就会出现此问题。

您可能更愿意在原位运行脚本,而不是更改其他脚本的调用路径,即将 $PWD 设置为脚本的目录。最简单的方法是使用 Windows 资源管理器 中的右键单击上下文菜单,通过 Windows shell 在 PowerGUI 中打开脚本。

This problem arises when you browse to the script you are working on from within PowerGUI.

Instead of changing the invocation paths to the other scripts you may prefer to run the script in-situ, i.e. with $PWD set to the directory of the script. This is most easily done by opening the script in PowerGUI through the Windows shell by using by the right-click context menu in Windows Explorer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文