从C#运行PowerShell脚本到Pull Git存储库

时间:2013-05-17 16:37:19

标签: c# git powershell windows-services

第一次在这里问一个问题,因为我对这个问题感到有些困惑。我有一个Powershell脚本来提取git存储库:

git.exe --git-dir=C:\ReleaseStaging\QABuild\.git pull --progress "origin"

如果我在powershell中执行此脚本,该脚本可以正常工作。

下一步是获取一个c#windows服务来执行这个powershell脚本。该命令的输出是“'W:/ QABuild'似乎不是git存储库。致命:无法从远程存储库读取。请确保您具有正确的访问权限并且存储库已存在。”

“W”驱动器是我的git存储库的映射驱动器,C:\ ReleaseStaging \ QABuild是我在本地克隆存储库的地方。

我的下一个测试是在ConsoleApplication中运行相同的c#代码,它运行正常。所以我认为我的Windows服务需要在不同的凭据下运行才能看到W驱动器,但由于我得到了相同的错误,因此无法在我的本地帐户下运行。

以下是我在Windows服务中使用的代码:

        using(RunspaceInvoke invoker = new RunspaceInvoke()) {
            invoker.Invoke("Set-ExecutionPolicy Unrestricted");
        }

        string cmdArg = String.Format(@"C:\ReleaseStaging\PullChanges.ps1");

        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.ApartmentState = System.Threading.ApartmentState.STA;
        runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
        runspace.Open();

        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(cmdArg);

        pipeline.Commands[0].MergeMyResults(
          PipelineResultTypes.Error, PipelineResultTypes.Output);

        Collection<PSObject> results = pipeline.Invoke();
        var error = pipeline.Error.ReadToEnd();
        runspace.Close();

有关如何从Windows服务中执行的PowerShell脚本中获取git存储库的任何想法吗?

1 个答案:

答案 0 :(得分:0)

问题是git pull隐式使用映射驱动器。在Windows服务中使用映射的网络驱动器是非常困难的。

有关如何操作的一些提示,请参阅this answer。最简单的方法可能是使用UNC路径而不是映射驱动器。