Search code examples
powershellf#chocolateyf#-fakef#-fake-4

F# FAKE will not execute powershell script file


Update: The actual cause of the issue was the '\' being round the wrong way, should have been './GenerateNuspecFile.ps1' - weird thing with F# parsing.


I am unable to run a ps1 script file from F# FAKE.

Question here which works well for running a chocolatey cpack

#r "packages/FAKE.3.12.2/tools/FakeLib.dll"
#r "System.Management.Automation"
#r "System.Core.dll"

open Fake
open System.Management.Automation
...

Target "GenerateNuspecFile" <| fun _ -> 
    PowerShell.Create()
        .AddScript("Invoke-Command -ScriptBlock { cd './chocolatey' | iex .\GenerateNuspecFile.ps1 }")
        .Invoke()
        |> Seq.iter (printfn "%O")
...

This is not actually running the script file that I am trying to run. Set-ExecutionPolicy is "unrestricted"


Solution

  • Your powershell script has some issues, and would not work even if run from the command line.

    In particular, cd .\somedir | iex .\SomeScript.ps1 is nonsensical - you are piping the result of a cd (which is $null, since cd doesn't return anything) to Invoke-Expression, which is basically saying "run this $null as a script.". Invoke-Command should not be needed at all.

    If your goal is simply to run GenerateNuSpecFile.ps1, your script should just be:

    .AddScript("cd '<somepath>'; .\GenerateNuspecFile.ps1")
    

    Or

    .AddScript("& '<somepath>\GenerateNuspecFile.ps1'")