C#通过代码调用PowerShell

发布时间:2016-12-31 7:30:48编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"C#通过代码调用PowerShell ",主要涉及到C#通过代码调用PowerShell 方面的内容,对于C#通过代码调用PowerShell 感兴趣的同学可以参考一下。


       var
userId = "MyAccount@XXXXX.partner.onmschina.cn"; var tenantId = "XXXXX-ca13-4bcb-840d-d3c1638f63b1"; var tenantDomain = "XXXX.partner.onmschina.cn"; var subscriptionId = "XXXXXX-1fb0-4df7-8737-11b8d6bf88ed"; var subscriptionName = "ZS0_Sub_OTR"; WindowsAzure.Commands.Common.AzureRmProfileProvider.Instance.Profile = new AzureRMProfile("D:\\XXXX.json"); var azureAccount = new AzureAccount() { Id = userId, Type = AzureAccount.AccountType.User }; var env = AzureEnvironment.PublicEnvironments["AzureChinaCloud"]; var subscript = new AzureSubscription() { Id = Guid.Parse(subscriptionId), Name = subscriptionName }; var azureContext = new AzureContext(subscript, azureAccount, env, new AzureTenant { Domain = tenantDomain, Id = Guid.Parse(tenantId) }); WindowsAzure.Commands.Common.AzureRmProfileProvider.Instance.Profile.Context = azureContext; WindowsAzure.Commands.Common.AzureRmProfileProvider.Instance.Profile.Save(); System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create(System.Management.Automation.RunspaceMode.NewRunspace); ps.Commands.Clear(); ps.AddCommand("Login-AzureRmAccount"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-EnvironmentName", "AzureChinaCloud"); var objs = ps.Invoke(); foreach (var item in objs) { Console.WriteLine(item); } ps.Commands.Clear(); ps.Commands.AddCommand("Set-AzureRmContext"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-SubscriptionName", "ZS0_Sub_OTR"); var sub = ps.Invoke(); //ps.Commands.Clear(); //ps.Commands.AddCommand("Save-AzureRmProfile"); //ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-Path", "D:\\XXXX.json"); //sub = ps.Invoke(); ps.Commands.Clear(); ps.Commands.AddCommand("Get-AzureRmKeyVault"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-VaultName", "SxzKeyVault"); var result = ps.Invoke(); ps.Commands.Clear(); ps.Commands.AddCommand("New-AzureRmKeyVault"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-VaultName", "SxzKeyVault3"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-ResourceGroupName", "YungoalTest"); ps.Commands.Commands[ps.Commands.Commands.Count - 1].Parameters.Add("-Location", "ChinaNorth"); result = ps.Invoke();


上一篇:jquery插件treetable使用
下一篇:Arduino 极速入门系列 - 光控灯(1)- 关于理论和 LED 那些事

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款