我有一個調用另一個腳本(帶參數)的腳本。被調用的腳本包含Install-WindowsFeature
cmdlet。當我運行該腳本,被調用的腳本運行,但返回以下錯誤:從被調用的腳本運行cmdlet的Powershell
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
當然,我可以運行該cmdlet從PowerShell控制檯就好了。我也可以從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上下文中運行它。文章解釋了爲什麼我認爲這很重要。
運行Windows Server 2012 R2
以管理員身份運行?或者可能提供憑據來運行它? Invoke-Command -Credential – Kage
@Kage我也嘗試過。不知道該cmdlet是否不可見,或者是否會導致拒絕訪問錯誤,但行爲是相同的,無論如何。 – McKenning
您是否曾嘗試在if語句之前的script.ps1中運行'import-module servermanager'? –