第一次在这里问问题,因为我对这个问题有点难过。我有一个 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/