2015-11-30 116 views
1

嘗試使用F#FAKE運行powershell腳本,但沒有任何反應......沒有錯誤,目標加載但實際上沒有運行。F#運行Powershell腳本作爲目標

// include Fake lib 
#r "packages/FAKE/tools/FakeLib.dll" 
#r "System.Management.Automation" 

open System 
open System.IO 
open System.Diagnostics 
open System.Management.Automation 

    Target "Powershell" <| fun _ -> 
    PowerShell.Create() 
     .AddScript("& 'build-database.ps1'") 
     .AddParameter("BuildVersion", version) 
     .AddParameter("Debug", "") 
     .Invoke() 
     |> Seq.iter (printfn "%O") 

// Dependencies 
"Clean" 
    ==> "Powershell" 

// start build 
RunTargetOrDefault "Powershell" 

我錯過了什麼嗎?沒有任何錯誤,我不確定問題是什麼。

*更新*

這是PowerShell腳本我與測試,假什麼都不做。

New-Item c:\AnEmptyFile.txt -ItemType file 
+0

你怎麼知道沒有什麼是運行?你可以添加一些跟蹤到'build-database.ps1'來看看它在做什麼?那麼你可以在FAKE腳本中的'Target「Powershell」'函數中添加一些跟蹤嗎? –

+0

PowerShell腳本在文件夾中創建一個文本文件。我從命令行運行腳本並且它工作,文件被創建。當使用FAKE時,輸出表示目標已成功完成...儘管腳本中沒有任何內容輸出,也沒有創建文件。 – devfunkd

+0

它總是創建相同的文件還是取決於參數?你能否始終在預定義的絕對位置創建相同的文件並查看是否會發生? –

回答

1

我終於想出了另一種方法來做到這一點。如果其他人需要從F#FAKE中調用powershell腳本,以下方法適用於我。

Target "Powershell" (fun _ -> 

     let p = new System.Diagnostics.Process();    
     p.StartInfo.FileName <- "cmd.exe"; 
     p.StartInfo.Arguments <- ("/c powershell -ExecutionPolicy Unrestricted .\\script.ps1) 
     p.StartInfo.RedirectStandardOutput <- true 
     p.StartInfo.UseShellExecute <- false 
     p.Start() |> ignore 

     printfn "Processing..." 
     printfn "%A" (p.StandardOutput.ReadToEnd()) 
     printfn "Finished" 
) 
0

它可以使用Shell.Execute和調用powershell.exe

Target "ImportDb" (fun _ -> 
    Shell.Exec("powershell.exe", "-NoProfile -ExecutionPolicy Bypass -File script.ps1") |> ignore)