当前位置: 首页 > 编程语言 > C#

无法从C#应用程序调用azurepowershellcmdletShare

时间:2023-04-10 19:14:26 C#

Failedtocallazurepowershellcmdletfromc#application我的powershell脚本就是这样做的,当从azurepowershell命令行执行时,它就像一个魅力。当我尝试从C#应用程序调用相同的脚本时,它失败了。这是我的代码:internalvoidRunPowerShellScript(stringscriptPath,Dictionaryarguments){RunspaceConfigurationrunspaceConfiguration=RunspaceConfiguration.Create();运行空间runspace=RunspaceFactory.CreateRunspace(runspaceConfiguration);运行空间.Run();RunspaceInvokescriptInvoker=new;流水线pipeline=runspace.CreatePipeline();//以下是添加带参数的新脚本的方法CommandmyCommand=newCommand(scriptPath,true);foreach(参数中的var参数){myCommand.Parameters.Add(newCommandParameter(argument.Key,argument.Value));}pipeline.Commands.Add(myCommand);varresults=pipeline.Invoke();foreach(varpsObjectinresults){_view.PrintOutput(psObject.ToString());我还关注了其他线程,例如:ExecutePowerShellscriptfromC#withcommandlinearguments,以及:Passingargumentstopowershellfromc#butnothingseemstowork。我收到以下错误:无法验证参数“PublishSettingsFile”的参数。参数为null或空。提供一个非null或空参数,然后重试该命令。剧本:Param($serviceName="",$storageAccountName="",$packageLocation="",$cloudConfigLocation="",$environment="",$deploymentLabel="",$timeStampFormat="g",$alwaysDeleteExistingDeployments=1、$enableDeploymentUpgrade=1,$selectedsubscription="default",$subscriptionDataFile="")functionPublish(){#Set-ExecutionPolicyRemoteSignedSet-AzureSubscription-SubscriptionName"WindowsAzureMSDN–VisualStudioProfessional"-CurrentStorageAccount$storageAccountName$deployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot-ErrorVariablea-ErrorActionsilentlycontinueif($a[0]-ne$null){Write-Output"$(Get-Date–f$timeStampFormat)-未检测到部署.创建新部署。”}#checkforexistingdeployment然后根据$alwaysDeleteExistingDeployments和$enableDeploymentUpgrade布尔变量升级、删除+部署或取消if($deployment.Name-ne$null){switch($alwaysDeleteExistingDeployments){1{switch($enableDeploymentUpgrade){1#Updatedeploymentinplace(通常更快,更便宜,不会破坏VIP){Write-Output"$(Get-Date–f$timeStampFormat)-Deploymentexistsin$服务名称。升级部署。”UpgradeDeployment}0#Deletethencreatenewdeployment{Write-Output"$(Get-Date–f$timeStampFormat)-$servicename中存在部署。删除部署。"DeleteDeploymentCreateNewDeployment}}#switch($enableDeploymentUpgrade)}0{Write-Output"$(Get-Date–f$timeStampFormat)-错误:$servicename中存在部署。脚本执行已取消。"exit}}#switch($alwaysDeleteExistingDeployments)}else{CreateNewDeployment}SwapVip}functionDeleteDeployment(){write-progress-id2-activity"删除部署"-Status"进行中"Write-Output"$(Get-Date–f$timeStampFormat)-删除部署:进行中"#WARNING-始终强制删除$removeDeployment=Remove-AzureDeployment-Slot$slot-ServiceName$serviceName-Forcewrite-progress-id2-activity"DeletingDeployment:Complete"-completed-Status$removeDeploymentWrite-Output"$(Get-Date–f$timeStampFormat)-删除部署:完成"}functionStartInstances(){write-progress-id4-activity"StartingInstances"-status"Inprogress"Write-Output"$(Get-Date–f$timeStampFormat)-StartingInstances:Inprogress"$部署=Get-AzureDeployment-ServiceName$serviceName-Slot$slot$runstatus=$deployment.Statusif($runstatus-ne'Running'){$run=Set-AzureDeployment-Slot$slot-ServiceName$serviceName-StatusRunning}$deployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot$oldStatusStr=@("")*$deployment.RoleInstanceList.Count同时(-not(AllInstancesRunning($deployment.RoleInstanceList))){$i=1foreach($$deployment.RoleInstanceList中的角色实例){$instanceName=$roleInstance.InstanceName$instanceStatus=$roleInstance.InstanceStatusif($oldStatusStr[$i-1]-ne$roleInstance.InstanceStatus){$oldStatusStr[$i-1]=$roleInstance.InstanceStatus写入输出“$(Get-Date–f$timeStampFormat)-启动实例'$instanceName':$instanceStatus"}write-progress-id(4+$i)-activity"启动实例'$instanceName'"-status"$instanceStatus"$i=$i+1}sleep-秒1$deployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot}$i=1foreach($roleInstancein$deployment.RoleInstanceList){$instanceName=$roleInstance.InstanceName$instanceStatus=$roleInstance.InstanceStatusif($oldStatusStr)[$i-1]-ne$roleInstance.InstanceStatus){$oldStatusStr[$i-1]=$roleInstance.InstanceStatusWrite-Output"$(Get-Date–f$timeStampFormat)-起始实例'$instanceName':$instanceStatus"}$i=$i+1}$deployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot$opstat=$deployment.Status写-progress-id4-activity"StartingInstances"-completed-status$opstatWrite-Output"$(Get-Date–f$timeStampFormat)-StartingInstances:$opstat"}函数AllInstancesRunning($roleInstanceList){foreach($roleInstancein)$roleInstanceList){if($roleInstance.InstanceStatus-ne"ReadyRole"){return$false}}return$true}functionSwapVip(){Write-Output"$(Get-Date–f$timeStampFormat)-交换生产和暂存为$servicename。”Move-AzureDeployment-ServiceName$servicename}functionCreateNewDeployment(){write-progress-id3-activity"CreatingNewDeployment"-Status"Inprogress"Write-Output"$(Get-Date–f$timeStampFormat)-创建新部署:进行中"$opstat=New-AzureDeployment-Slot$slot-Package$packageLocation-Configuration$cloudConfigLocation-label$deploymentLabel-ServiceName$serviceName$completeDeployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot$completeDeploymentID=$completeDeployment.deploymentidwrite-progress-id3-activity"CreatingNewDeployment"-completed-Status"Complete"Write-Output"$(Get-Date–f$timeStampFormat)-创建新部署:完成,部署ID:$completeDeploymentID"StartInstances}functionUpgradeDeployment(){write-progress-id3-activity"UpgradingDeployment"-Status"Inprogress"Write-Output"$(Get-Date–f$timeStampFormat)-升级部署:正在进行中"#performUpdate-部署$setdeployment=Set-AzureDeployment-Upgrade-Slot$slot-Package$packageLocation-Configuration$cloudConfigLocation-label$deploymentLabel-ServiceName$serviceName-Force$completeDeployment=Get-AzureDeployment-ServiceName$serviceName-Slot$slot$completeDeploymentID=$completeDeployment.deploymentidwrite-progress-id3-activity"UpgradingDeployment"-completed-Status"Complete"Write-Output"$(Get-Date–f$timeStampFormat)-升级部署:完成,部署ID:$completeDeploymentID"}Import-ModuleAzure$pubsettings=$subscriptionDataFileImport-AzurePublishSettingsFile$pubsettingsSet-AzureSubscription-CurrentStorageAccount$storageAccountName-SubscriptionName$selectedsubscription#setremainingenvironmentvariablesforAzurecmdlets$subscription=Get-AzureSubscription$selectedsubscription$subscriptionname=$subscription.subscriptionname$subscriptionid=$subscription.subscriptionid$slot=$environmentWrite-Output"$(Get-Date–f$timeStampFormat)-Azure云服务部署脚本启动。"Write-Output"$(Get-Date–f$timeStampFormat)-为订阅ID为$subscriptionid的$subscriptionname准备部署$deploymentLabel。–f$timeStampFormat)-使用URL$deploymentUrl创建云服务。"Write-Output"$(Get-Date–f$timeStampFormat)-Azure云服务部署脚本完成。”我相信您传递publishsettings文件的方式导致了问题。传递publishsettings文件时,您必须添加引号,如果没有引号,则会出现异常将被触发。这是基于您的代码的代码,我通过使用(")引号传递publishsetting文件对其进行了测试,它工作正常:privatevoidbutton1_Click(objectsender,EventArgse){DictionarymyDict=newDictionary();myDict.Add("-subscriptionDataFile",""C:\InstallBox\asc.publishsettings"");RunPowerShellScript("C:\InstallBox\testcode.ps1",myDict);}internalvoidRunPowerShellScript(stringscriptPath,Dictionaryarguments){RunspaceConfigurationrunspaceConfiguration=}RunspaceConfiguration.Create();Runspacerunspace=RunspaceFactory.CreateRunspace(runspaceConfiguration);runspace.Open();RunspaceInvokescriptInvoker=newRunspaceInvoke(runspace);Pipelinepipeline=runspace.CreatePipeline();//这是添加新脚本的方法argumentsCommandmyCommand=newCommand(scriptPath);foreach(varargumentinarguments){myCommand.Parameters.Add(newCommandParameter(argument.Key,argument.Value));}pipeline.Commands.Add(myCommand);varresults=pipeline.Invoke();foreach(varpsObjectinresults){.........}}我的testcode.ps1如下:Param([string]$subscriptionDataFile)Import-ModuleAzureImport-AzurePublishSettingsFilesubscriptionDataFileGet-AzureSubscription为了确保我能看到你的问题,如果我把字典键值更改为以下内容(不带引号),我得到与您描述的完全相同的错误:myDict.Add("-subscriptionDataFile","C:\InstallBox\asc.publishsettings");那么,PublishSettings文件的正确传递方式如下:以上是C#学习教程:从c#应用程序调用azurepowershellcmdlet失败共享所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——myDict.Add("-subscriptionDataFile",""C:\InstallBox\asc.publishsettings"");本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: