首页 > 代码库 > 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();
C#通过代码调用PowerShell
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。