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();
时间: 2024-10-14 12:21:22

C#通过代码调用PowerShell的相关文章

C#中调用PowerShell代码

在C#中调用PowerShell代码,很多时候Add是不好使的!要用AddScript!记录一下! using (Runspace runspace = RunspaceFactory.CreateRunspace()) { try { runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); //必须要用AddScript. pipeline.Commands.AddScript("Add-PSSnapin Microsof

Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import

存储过程--分页与C#代码调用

存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author:<Author,,QiangWang> -- Create date: <Create Date,,> -- Description:<Description,分页,> -- ===================================

dzx2.5 template\default\forum\viewthread_node.htm代码调用解放(和我一样的菜鳥版)

<!--{block authorverifys}--> <!--{if $_G['setting']['verify']['enabled']}--> <!--{loop $_G['setting']['verify'] $vid $verify}--> <!--{if $verify['available'] && $verify['showicon']}--> <a href="home.php?mod=spacecp&

线程运行栈StackTrace用法,代码调用树查看,出错代码位置信息提示,代码所在类名包名文件名查看

1.代码调用树查看 2.出错代码位置信息提示 功能函数: /** 在LogCat中输出提示信息info,并给出输出该信息在代码中的完整调用树 */ public static void MessageWithSrcTree(String info) { StackTraceElement[] elem = Thread.currentThread().getStackTrace(); //从当前位置,获取代码调用堆栈 for(StackTraceElement e : elem) { Strin

x2.5 discuz.htm代码调用解说(第一讲)

<!--{subtemplate common/header}--> 复制代码调用 common/header.htm 头部文件 <div id="pt" class="bm cl"> <!--{if empty($gid) && $announcements}--> <div class="y"> <div id="an"> <dl class

ASP.NET中前台javascript与后台代码调用

ASP.NET中前台javascript与背景代码调用 1如安正在JavaScript访问C#函数? 2.如安正在JavaScript访问C#变量? 3.如安正在C#中访问JavaScript的已经有变量? 4.如安正在C#中访问JavaScript函数? 标题1谜底以下: javaScript函数中实施C#代码中的函数: 方法一:一.起首成立一个按钮,正在背景将调用或许搞定的内容写入button_click中; 二.正在前台写一个js函数,内容为document.getElementById(

存储过程具体过程以及sql数据库调用和程序代码调用

1.存储过程,无参数的存储过程 创建无参数存储存储过程 Create Procedure DCEMREMR_TEMPLATEAs SELECT TOP 10 [FILENAME],[FILETITLE],[FILECONTENT] from [DCEMR].[dbo].[EMR_TEMPLATE]; 调用无参数存储存储过程 sql 数据库中的额调用  exec DCEMREMR_TEMPLATE: sql程序代码调用 //无参数存储过程 string connecting = "Data Sou

C#调用PowerShell脚本

今天通过一个小例子,学习了C#如何调用PowerShell脚本文件的Function以及传参. private bool CallPowershell(string outputFile) { string ddcHost = "test"; RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRun