Je suis en train de passer un paramètre à PowerShell de C# application web, je suis sans cesse une erreur:paramètres de passage à Powershell de C#
Reason = {"The term 'Param($ds)\r\n\r\n$ds\r\n\r\n\r\n' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."}
scénario Mon Powershell est la suivante:
Param($ds)
write-host $ds
Mon
C# est:
protected void drpCluster_SelectedIndexChanged(object sender, EventArgs e)
{
// Looping through all the rows in the GridView
foreach (GridViewRow row in GridVMApprove.Rows)
{
if (row.RowState == DataControlRowState.Edit)
{
// create dynamic dropDowns for datastores
DropDownList drpDatastore = (DropDownList)row.FindControl("drpDatastore");
DropDownList drpCluster = (DropDownList)row.FindControl("drpCluster");
var Datacenter = "'" + drpCluster.SelectedValue + "'";
strContent = this.ReadPowerShellScript("~/scripts/Get-DatastoresOnChange.ps1");
this.executePowershellCommand(strContent, Datacenter);
populateDropDownList(drpDatastore);
}
}
}
public string ReadPowerShellScript(string Script)
{
// Read script
StreamReader objReader = new StreamReader(Server.MapPath(Script));
string strContent = objReader.ReadToEnd();
objReader.Close();
return strContent;
}
private string executePowershellCommand(string scriptText, string scriptParameters)
{
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("vmware.vimautomation.core", out snapInException);
Runspace RunSpace = RunspaceFactory.CreateRunspace(rsConfig);
RunSpace.Open();
Pipeline pipeLine = RunSpace.CreatePipeline();
Command scriptCommand = new Command(scriptText);
pipeLine.Commands.AddScript(scriptText);
if (!(scriptParameters == null))
{
CommandParameter Param = new CommandParameter(scriptParameters);
scriptCommand.Parameters.Add(Param);
pipeLine.Commands.Add(scriptCommand);
}
// Execute the script
Collection<PSObject> commandResults = pipeLine.Invoke();
// Close the runspace
RunSpace.Close();
// Convert the script result into a single string
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
foreach (PSObject obj in commandResults)
{
stringBuilder.AppendLine(obj.ToString());
}
OutPut = stringBuilder.ToString();
return OutPut;
}
J'ai suivi quelques autres threads mais ne peut pas script à exécuter. Mes scripts PowerShell s'exécutent si je l'exécute à partir de la console PowerShell en appelant simplement le script et un paramètre. Si je supprime Param($ds)
du script PowerShell, il ne se trompe pas.
Quelqu'un peut-il aider?
Merci.
Merci pour la réponse, mais cela ne semble pas sonner vrai. si j'ai un script appelé test.ps1 avec les éléments suivants Param ($ msg) write-host $ msg puis appelez avec ./test.ps1 Bonjour Je ne suis bonjour écrit à la console. J'ai aussi essayé d'utiliser $ args [0] dans le script avec le même résultat. si je mets & {param ($ ds); $ ds | hors fichier c: \ scripts \ testparam.txt} dans le script que j'appelle je reçois toujours la même erreur. –
Vous devez transmettre les arguments au script comme suit: & {param ($ ds); $ ds} 42 Cela se traduit par 42 passesd au scriptblock. Coller le script avec AddScript ne crée pas implicitement un fichier ps1. C'est comme si vous tapiez: ps c: \> param ($ ds); $ ds directement à l'invite. C'est sans signification. Avoir du sens? – x0n
cool oui maintenant. Je vous remercie. –