我有代碼從包含很多powershell cmdlet的.ps1文件運行powershell cmdlet ... when我執行它,我得到這個例外代碼來運行cmps的.ps1文件中的powershell cmdlet(函數?)
「新BuildCVFromSql」不被識別爲一個 cmdlet,函數,腳本文件或可操作的程序的名稱術語。檢查名稱的拼寫 ,或者如果包含路徑,請確認路徑爲 正確,然後重試。
private void RunPowerShellCommandToBuildCV()
{
string BigWindow = "";
string FileNamePopup = "";
TestPopup(ref BigWindow, ref FileNamePopup);
string psScriptPath = @"D:\Project Files\CIS3G\Webapp\_Powershell\JcdcCv.psm1";
string psScript = string.Empty;
if(File.Exists(psScriptPath))
psScript = File.ReadAllText(psScriptPath);
else
throw new FileNotFoundException("Wrong path for the script file");
// init the powershell runspace and pipeline - EWB
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
// set execution policy - EWB
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runSpace.CreatePipeline();
//i tried loading it both of the ways below and no joy load the script from the string - EWB
//pipeline.Commands.AddScript(psScript);
//load as file - EWB
pipeline.Commands.AddScript(psScriptPath, false);
//add the outstring command to the pipeline.
pipeline.Commands.Add("Out-String");
Command myCommand = new System.Management.Automation.Runspaces.Command("New-BuildCVFromSql");
CommandParameter SqlOrExcelFile = new CommandParameter("SqlOrExcelFile", @"C:\Documents and Settings\Brown.Ericw\My Documents\tempeval.sql");
CommandParameter Js = new CommandParameter("Js", "JCDC");
CommandParameter FileName = new CommandParameter("FileName", @"Evaluation\EvaluationFormPartialListVM");
myCommand.Parameters.Add(SqlOrExcelFile);
myCommand.Parameters.Add(Js);
myCommand.Parameters.Add(FileName);
pipeline.Commands.Add(myCommand);
Collection<PSObject> output = pipeline.Invoke();
//foreach (PSObject psObject in output)
//{
//System.Diagnostics.Debug.WriteLine ("Object name: " + psObject.);
//}
}
所以顯然我沒有正確加載腳本文件?在powershell ISE/IDE中,我必須在執行任何命令之前運行腳本,是我做錯了什麼?
我只是想把接口放在一堆由somone寫的powershell腳本上,所以我不能真的改變他的腳本文件,因爲他們在其他人的別處使用...
在一側的.ps1文件我試圖打電話給小命令正是如此定義:
# Create CV Method #
function New-BuildCVFromSql {
param([Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$SqlFile,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$js,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$FileName)
...
附錄:
Problem with calling a powershell function from c#
:使用PowerShell的而不是每個此帖一管道也試過這樣,但我得到了同樣的異常:
private void RunPowerShellCommandToBuildCV()
{
string BigWindow = "";
string FileNamePopup = "";
TestPopup(ref BigWindow, ref FileNamePopup);
string psScriptPath = @"D:\Project Files\CIS3G\Webapp\_Powershell\JcdcCv.psm1";
string psScript = string.Empty;
if (File.Exists(psScriptPath))
psScript = File.ReadAllText(psScriptPath);
else
throw new FileNotFoundException("Wrong path for the script file");
// init the powershell runspace and pipeline - EWB
using (Runspace runSpace = RunspaceFactory.CreateRunspace())
{
runSpace.Open();
// set execution policy - EWB
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
//Pipeline pipeline = runSpace.CreatePipeline();
PowerShell ps = PowerShell.Create();
ps.Runspace = runSpace;
//load as file - EWB
ps.AddScript(psScriptPath);
ps.AddCommand("New-BuildCVFromSql").AddParameters(new Dictionary<string, string>()
{
{ "SqlOrExcelFile", @"C:\tempeval.sql" },
{ "Js", "JCDC" },
{ "FileName", @"Evaluation\EvaluationFormPartialListCV" }
});
foreach (PSObject result in ps.Invoke())
{
Debug.WriteLine ("Object : " + result);
}
}
附錄2:
刪除所有命令的東西(因爲他們只爲內置的cmdlet,而不是用戶自定義函數的工作),並這樣做反而似乎是在調用代碼,儘管當它運行時沒有任何依賴關係被覆蓋。仍然在研究這些。
ps.AddScript(@"New-BuildCVFromSql 'C:\Documents and Settings\Brown.Ericw\My Documents\tempeval.sql' JCDC 'Evaluation\EvaluationFormPartialListCV'");
似乎在調用代碼,儘管在運行時不會加載任何依賴關係..仍在研究這個問題。但是,如果它是從它的工作ISE ....運行
任何具有完整源代碼的最終解決方案? – Kiquenet
Down投票贊成不發佈剪切和粘貼答案?哇,這是苛刻的......它只有1行不同,並在評論中。 –