简体   繁体   English

从C#运行PowerShell脚本以提取Git存储库

[英]Running a PowerShell script from C# to Pull Git Repository

First time asking a question here, as I'm a bit stumped on this one. 第一次在这里问一个问题,因为我对此有些困惑。 I have a Powershell script to pull a git repository: 我有一个Powershell脚本来提取git存储库:

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

The script works fine if I execute this in powershell. 如果我在powershell中执行此脚本,则脚本运行良好。

The next step was to get ac# windows service to execute this powershell script. 下一步是获取ac#Windows服务以执行此Powershell脚本。 The output of the command is "'W:/QABuild' does not appear to be a git repository. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists." 命令的输出为“'W:/ QABuild'似乎不是git存储库。致命:无法从远程存储库读取。请确保您具有正确的访问权限并且该存储库存在。”

The "W" drive is a mapped drive to my git repository and C:\\ReleaseStaging\\QABuild is where I clone the repository locally. “ W”驱动器是到我的git存储库的映射驱动器,而C:\\ ReleaseStaging \\ QABuild是我在本地克隆存储库的位置。

My next test was to run the same c# code in a ConsoleApplication and it worked fine. 我的下一个测试是在ConsoleApplication中运行相同的C#代码,并且运行良好。 So I figured that my Windows service needed to run under different credentials to see that W drive, but that didn't work running under my local account either as I got the same error. 因此,我认为我的Windows服务需要在不同的凭据下运行才能看到W驱动器,但是由于遇到相同的错误,因此在我的本地帐户下运行也不起作用。

Here is the code that I'm using in my windows service: 这是我在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();

Any thoughts as to how to get a git repository to pull from a powershell script executed in a windows service? 关于如何从Windows服务中执行的Powershell脚本中提取git存储库的任何想法?

The problem is that git pull implicitly is using the mapped drive. 问题在于git pull隐式使用映射驱动器。 Using a mapped network drive in a windows service is notoriously difficult. 在Windows服务中使用映射的网络驱动器非常困难。

See this answer for some tips on how to do it. 有关此操作的一些提示,请参见此答案 The simplest method is probably to use a UNC path instead of a mapped drive. 最简单的方法可能是使用UNC路径而不是映射驱动器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM