c# - 从 C# 运行 PowerShell 脚本以 pull Git 存储库

标签 c# git powershell windows-services

第一次在这里问问题,因为我对这个问题有点难过。我有一个 Powershell 脚本来 pull 一个 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 存储库有什么想法吗?

最佳答案

问题是 git pull 隐含地使用映射驱动器。在 Windows 服务中使用映射网络驱动器是出了名的困难。

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

关于c# - 从 C# 运行 PowerShell 脚本以 pull Git 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16613998/

相关文章:

c# - WPF 设计器中标记扩展参数的设计时检查

c# - Linq groupBy IGrouping 以简单/干净的方式列出 <>

git - 在 --skip-worktree 之后无法切换分支

azure - 在需要 Azure 身份验证的应用程序上调用 RestMethod

c# - 当 "break all"没有响应时,如何调试无响应的 .Net 服务?

xcode - Crashlytics git 管理

eclipse - 如何为新的 eclipse (neon) java 项目初始化 git

powershell - Azure 外部负载均衡器 - 使用 PowerShell 添加 LB 规则

powershell - 为什么 powershell(ise) 有时会打印出我执行的代码?

c# - 在 Mvc 4 中使用每类型表继承将对象添加到数据库