I believe that the way you are passing the publishsettings file is causing the problem. When passing the publishsettings file, you must add the quotes and if there are no quotes you will hit the exception.
Following is the code which is based on your code and I tested it by passing publishsetting file with quotes using (\") and it works fine:
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, string> myDict= new Dictionary<string, string>();
myDict.Add("-subscriptionDataFile", "\"C:\\InstallBox\\asc.publishsettings\"");
RunPowerShellScript("C:\\InstallBox\\testcode.ps1", myDict);
}
internal void RunPowerShellScript(string scriptPath, Dictionary<string, string> arguments)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command(scriptPath);
foreach (var argument in arguments)
{
myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value));
}
pipeline.Commands.Add(myCommand);
var results = pipeline.Invoke();
foreach (var psObject in results)
{
.........
}
}
And my testcode.ps1 is as below:
Param( [string]$subscriptionDataFile)
Import-Module Azure
Import-AzurePublishSettingsFile subscriptionDataFile
Get-AzureSubscription
To make sure I can see your problem, if i change the dictionary key value pair to as below (without quotes) I get exact same error as you described:
myDict.Add("-subscriptionDataFile", "C:\\InstallBox\\asc.publishsettings");
So the correct way to pass PublishSettings file is as below:
myDict.Add("-subscriptionDataFile", "\"C:\\InstallBox\\asc.publishsettings\"");