【发布时间】:2016-11-10 23:36:41
【问题描述】:
我有一个脚本调用另一个脚本(带参数)。被调用的脚本包含Install-WindowsFeature cmdlet。当我运行脚本时,被调用的脚本运行,但返回以下错误:
Install-WindowsFeature : The term 'Install-WindowsFeature' 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
当然,我可以从 PowerShell 控制台很好地运行 cmdlet。我还可以从 PowerShell 控制台运行调用的脚本,Install-WindowsFeature 工作正常。那么,这与从运行 cmdlet 的脚本调用脚本有关吗?这是我的调用代码:
$script = "C:\Path\script.ps1"
$argumentList = @()
$argumentlist += ("-Arg1", "value")
$argumentlist += ("-Arg2", "value")
$argumentlist += ("-Arg3", "value")
Invoke-Expression "$script $argumentList"
在被调用的脚本中,我调用了Install-WindowsFeature,如下:
if ($someValue) { Invoke-Command -ScriptBlock {Install-WindowsFeature -Name RSAT-AD-Tools} }
我也试过如下:
if ($someValue) { Install-WindowsFeature -Name RSAT-AD-Tools }
12/16/16 编辑:此脚本在使用 Sapien PowerShell Studio 构建的 GUI 中运行。当我将构建类型更改为“本机”时,它可以工作。我必须重置我的实验室才能检查,但我怀疑如果我只是在 x64 上下文中运行它,它也会运行。 This 文章解释了为什么我认为这很重要。
在 Windows Server 2012 R2 上运行
【问题讨论】:
-
以管理员身份运行?或者也许提供凭据来运行它? Invoke-Command -Credential
-
@Kage 我也试过了。不确定该 cmdlet 是否只是不可见或者是否会给出拒绝访问错误,但无论如何行为都是相同的。
-
您是否尝试在
if语句之前在 script.ps1 中运行import-module servermanager? -
@TonyHinkle 刚回到这个项目并将命令添加到 script.ps1 并给出错误“未加载指定的模块'ServerManager',因为在任何模块目录中都找不到有效的模块文件。”就像脚本的运行上下文看不到环境/文件系统一样。
-
我现在不知道该建议什么。我没有测试服务器来尝试复制它。我会为此悬赏,以便引起注意。
标签: powershell