我的 PowerShell 函数似乎未注册

我的 PowerShell 函数似乎未注册

我有一个 ps1 脚本,其中定义了两个函数:

function Invoke-Sql([string]$query) {
  Invoke-Sqlcmd -ServerInstance $Server -Database $DB -User $User -Password $Password -Query $query
}

function Get-Queued {
  Invoke-Sql "Select * From Comment where AwaitsModeration = 1"
}

然后我通过输入来调用 ps1 文件(它位于路径中的文件夹中,并且自动完成功能有效)

但是,我无法开始使用这些函数。我很困惑,因为当我将函数复制/粘贴到控制台时,一切都很好,它们可以工作。我的配置文件中也定义了一个函数,它可以工作。我哪里想错了,为什么我尝试做的事情不起作用?

答案1

为了使这些功能可用,您需要“dot source”脚本。“Dot Sourcing”在当前范围内运行脚本。

要“点源”脚本,您需要先放置一个句点,然后放置一个空格,然后放置脚本的完整路径或相对路径。

例如

. ./myscript.ps1

答案2

查看这个帖子关于从命令行运行 powershell 脚本。

相关内容