Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么powershell无法识别windows服务调用的脚本中的cmdlet?_C#_Azure_Powershell_Service - Fatal编程技术网

C# 为什么powershell无法识别windows服务调用的脚本中的cmdlet?

C# 为什么powershell无法识别windows服务调用的脚本中的cmdlet?,c#,azure,powershell,service,C#,Azure,Powershell,Service,我们有一个作为本地系统帐户运行的windows服务。它调用PowerShell脚本并检索输出以进行进一步处理。它在windows server 2016下就像一个魔咒,但现在需要移动到windows server 2012 R2。在此计算机上,它无法识别azure特定的cmdlet 我试图通过-Scope alluser安装特定的cmdlet。我们还以本地系统-用户的身份直接登录到PowerShell;它确实能够正确识别cmdlet(例如,添加AzurerAccount) C#: getVMs.

我们有一个作为本地系统帐户运行的windows服务。它调用PowerShell脚本并检索输出以进行进一步处理。它在windows server 2016下就像一个魔咒,但现在需要移动到windows server 2012 R2。在此计算机上,它无法识别azure特定的cmdlet

我试图通过
-Scope alluser
安装特定的cmdlet。我们还以
本地系统
-用户的身份直接登录到PowerShell;它确实能够正确识别cmdlet(例如,
添加AzurerAccount

C#:

getVMs.ps1:

$finalPassword = ConvertTo-SecureString -String $password -AsPlainText -Force

$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList $accountName, $finalPassword

$trash = Add-AzureRmAccount -Credential $psCred -TenantId $tenantID -ServicePrincipal`
我们不理解为什么cmdlet在server 2016上的相同环境下工作良好,如果我们直接以用户身份运行它们


任何提示都值得欣赏

我建议您在c#代码中直接导入包含cmdlet的模块

1.您可以使用此命令获取模块文件(我正在使用az模块,请随意更改为azureRm模块):

然后您可以看到模块文件路径,在我这边,它是“C:\Program Files\WindowsPowerShell\Modules\Az.Accounts\1.5.2\Az.Accounts”

2.在您的c#代码中:


我建议在c#代码中直接导入包含cmdlet的模块

1.您可以使用此命令获取模块文件(我正在使用az模块,请随意更改为azureRm模块):

然后您可以看到模块文件路径,在我这边,它是“C:\Program Files\WindowsPowerShell\Modules\Az.Accounts\1.5.2\Az.Accounts”

2.在您的c#代码中:


如果将
导入模块AzureRM.Profile
添加到
getVMs.ps1
,会发生什么情况?Windows Server 2012上安装了什么
Azure RM PowerShell
版本<代码>添加AzurerAccount在版本
2.5.0
上不受支持,因此您可能必须更新
Azure RM Powershell
->AzureRM。配置文件安装在版本5.8.2中。将该语句添加到脚本中不会改变任何内容。如前所述,脚本本身在PowerShell窗口中运行良好。甚至作为运行服务的本地系统用户。这对我来说毫无意义。您能告诉我们您推断“它无法识别azure特定cmdlet”的完整错误吗?原始消息是德语,因此下面是我日志文件中的翻译消息:“术语”Add AzureRmAccount未识别为cmdlet、函数、脚本文件或可执行程序的名称。检查名称的拼写,或者路径是否正确(如果包含),然后重试“如果将
导入模块AzureRM.Profile
添加到
getVMs.ps1
,会发生什么情况?Windows Server 2012上安装了什么
Azure RM PowerShell
版本<代码>添加AzurerAccount在版本
2.5.0
上不受支持,因此您可能必须更新
Azure RM Powershell
->AzureRM。配置文件安装在版本5.8.2中。将该语句添加到脚本中不会改变任何内容。如前所述,脚本本身在PowerShell窗口中运行良好。甚至作为运行服务的本地系统用户。这对我来说毫无意义。您能告诉我们您推断“它无法识别azure特定cmdlet”的完整错误吗?原始消息是德语,因此下面是我日志文件中的翻译消息:“术语”Add AzureRmAccount未识别为cmdlet、函数、脚本文件或可执行程序的名称。请检查名称的拼写,或者路径是否正确(如果包含),然后重试。“我收到了与“Get AzureRmVm”命令相同的消息,谢谢,这确实很有帮助。必须添加几个模块,但现在效果很好!谢谢,确实有帮助。必须添加几个模块,但现在效果很好!
$finalPassword = ConvertTo-SecureString -String $password -AsPlainText -Force

$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList $accountName, $finalPassword

$trash = Add-AzureRmAccount -Credential $psCred -TenantId $tenantID -ServicePrincipal`
(Get-Module -Name az.accounts).Path
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] {"the full path of Az.Accounts.psd1, if it does not work, try full path of Az.Accounts.psm1"} );
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();     
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript(scriptBase + "getVMs.ps1");
var azureVMList = psInstance.Invoke();