How to execute more than one power shell command in a single .ps1 file

核能气质少年 提交于 2019-12-24 13:30:35

问题


I am trying to run more than one powershell command(with parameters in each command) in a single .ps1 file.

i.e. In the Azure powershell i need to run these 3 commands sequentially

Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force 
Import-AzurePublishSettingsFile 
New-AzureService

All the above 3 command receive different parameters. I wanted to keep them in a single .ps1 file.

I am trying as bellow, it is not working although not giving any exception

 using (PowerShell ps = PowerShell.Create())
{
 string Filepath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"path of .ps1 file");
 string Script = File.ReadAllText(Filepath);  

 IDictionary Param = new Dictionary<String, String>();
 Param.Add("–PublishSettingsFile", @"path of pubsetting file");
 Param.Add("-ServiceName", CloudServiceName);
 Param.Add("-Location", Location);

var result = 
PowerShell.Create().AddScript(Script).AddParameters(Param).Invoke();
}

来源:https://stackoverflow.com/questions/28185234/how-to-execute-more-than-one-power-shell-command-in-a-single-ps1-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!