bat文件在任务调度器中没有以通用用户的身份运行git push

sigwle7e  于 2024-01-04  发布在  Git
关注(0)|答案(1)|浏览(125)

我在task scheduler中设置了一个schedule,以普通用户的身份运行一个包含git命令的bat文件。
此bat文件包含以下命令:

cd C:\repo
call D:\path\to\git --git-dir=C:\repo\.git add --all > log.txt
call D:\path\to\git --git-dir=C:\repo\.git -c user.name="genericUser" -c user.email="[email protected]" commit --author="genericUser <[email protected]>" -m "message" >> log.txt
echo Pushing changes... >> log.txt
set GIT_TRACE=true
call D:\path\to\git --git-dir=C:\repo\.git push origin main >> log.txt
EXIT /B

字符串
任务调度器属性如下所示:

常规页签

  • 用户帐户设置为“genericUser”
  • 选择“无论用户是否登录都运行”
  • “以最高权限运行”已选中

操作页签

  • 操作设置为“启动程序”

在此操作的属性中:

  • 程序/脚本设置为C:\Window\System32\cmd.exe
  • 将参数设置添加到/c start "" "C:\repo\commit.bat"
  • 开始设置为C:\repo

Conditions页签

  • 默认检查到位
  • 唤醒计算机以运行此任务已选中

当我运行schedule时,它会运行所有的东西,但是当它点击git push命令时会卡住。
我已经为这个普通用户设置了windows凭据,并成功地以普通用户的身份推送。git config --global user.name和user.email也已经为这个普通用户设置好了。当在git bash中手动运行git命令时,git push上没有提示输入凭据。
我的global .gitconfig看起来像这样:

[credential]
    helper = manager
[user]
    name = genericUser
    email = [email protected]
[commit]
    name = genericUser
    email = [email protected]

ua4mk5z4

ua4mk5z41#

从评论中,您可以:

  • 使用git.exe的完整路径,而不是gitcall D:\path\to\git。这样可以确保正确的可执行文件被调用而不会产生歧义。
  • 添加2>&1,将所有命令的标准输出和错误输出都重定向到log.txt。这将捕获任何错误消息。
  • 使用cd /D C:\repo确保脚本将工作,即使当前目录是在不同的驱动器。
  • 删除批处理文件末尾的EXIT /B
  • 基于未设置HOMEDRIVEHOMEPATH的问题,显式设置变量。
  • 将任务队列参数更改为/D /C C:\repo\commit.bat,以更直接地调用批处理文件。

你的剧本应该是:

@echo off
cd /D C:\repo

:: Set environment variables explicitly for Git
set HOMEDRIVE=C:
set HOMEPATH=\Users\genericUser
set GIT_TRACE=true
set PATH=%PATH%;D:\path\to\git\bin

:: Capture both standard output and error output for all commands
D:\path\to\git.exe --git-dir=C:\repo\.git add --all >> log.txt 2>&1
D:\path\to\git.exe --git-dir=C:\repo\.git -c user.name="genericUser" -c user.email="[email protected]" commit --author="genericUser <[email protected]>" -m "message" >> log.txt 2>&1
echo Pushing changes... >> log.txt
D:\path\to\git.exe --git-dir=C:\repo\.git push origin main >> log.txt 2>&1

字符串
特别注意是谁在运行脚本:检查任务的安全选项,确保它是以正确的用户身份运行的。
最后一部分是问题的关键:使用GCM (Microsoft Git Credential Manager)(与git config --global credential.helper manager一起使用)意味着使用Windows vaultWindows Credential Manager)。
即使你在所说的保险库中输入了 * 另一个 * 帐户的凭据,也只有 * 你 *(你的当前帐户)才能读回这些凭据。
如果您的任务作为通用帐户运行,则需要将这些凭据存储在Runas /profile /user:Company\accountName CMD会话中。
在那里,你将能够store credentials,你的任务将能够读回。

相关问题