【问题标题】:PowerShell git push as a scheduled taskPowerShell git push 作为计划任务
【发布时间】:2018-12-19 14:45:47
【问题描述】:

我们为自动报告脚本设置了 Win Server 2016,以便在作为计划任务执行时将报告推送到 github。

当我以代理用户身份登录时,我可以毫无问题地运行此脚本。我已经启动了 sshagent 并且它正在运行。当作为计划任务运行时,该脚本在脚本的第二部分(git push)中终止。

我已尝试将 git-push 部分作为计划任务单独运行,但仍然无法运行(ssh 代理仍在运行)。我也可以毫无问题地在 git-bash 中运行它。

#git checkout the most recent vCenter list.
cd D:\virtualization-reporting
git checkout vcenters.csv
cd D:\scripts

#list of vCenters to be queried
$vcenters = import-csv D:\virtualization-reporting\vcenters.csv

#connect to vCenters, get templates, export to csv.
foreach ($vc in $vcenters){
    $creds = Get-VICredentialStoreItem -host $vc.vcenter -file D:\scripts\creds.xml -ErrorAction Ignore
    Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password
    foreach($dc in Get-Datacenter){
        foreach($cluster in Get-Cluster -Location $dc){
            Get-Template |
            Select Name,
            @{N='vCenter';e={$vc}},
            @{N='Cluster';E={$cluster.Name}},
            @{N='Path';e={$_.extensiondata.config.files.VmPathName}}|
            sort Name,vCenter,Cluster,Path|
            export-csv -append -path D:\virtualization-reporting\Template_Distribution_Report\Template_status-$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation
        }
    }

    #disconnects from each vCenter after gathering data and appeneding to csv
    disconnect-viserver * -confirm:$false
}

#change directory to the repo path on the POSH host.
cd D:\virtualization-reporting

#git merge output with GitHub 
$date = (get-date)
git checkout master
git pull
git add -A
git commit -m "Updated Template Distribution Report for $date"
git push

#exit PowerShell Session
Exit-PSSession

如果我不能在 PowerShell 中运行它,我会很高兴有一个在 POSH 或 git bash 中运行的计划任务来执行 git push。

谢谢。

【问题讨论】:

  • 这里没有问题。
  • 您的错误信息是什么?我猜您需要将 powercli 模块添加到顶部,因为您的计划任务将打开 powershell 而不是 powercli。 Get-Module -Name VMWare* -ListAvailable |导入模块

标签: git powershell github powercli


【解决方案1】:

首先,我正在运行通过检查源来验证的 ssh URL。

最终,我确定脚本在脚本的 git 部分失败。虽然我尝试过启动代理并以各种方式将密钥添加到代理,但它仍然死机。我发现以下方法可以始终如一地工作(即使在重新启动后)。

我从https://github.com/PowerShell/Win32-OpenSSH/releases 安装了 OpenSSH 下载,解压缩到 a 文件夹,然后运行脚本:install-sshd.ps1,它安装了 2 个 OpenSSH 服务。

然后我执行了以下步骤:

  1. 已生成新的 SSH 密钥
  2. 向代理添加了 SSH 密钥
  3. 将密钥添加到 GitHub
  4. 验证 SSH 是否正常运行 - ssh git@github.mycompany.com

脚本作为计划任务运行,没有问题。

这是重新启动后我能找到持久性的唯一方法。

【讨论】:

  • 感谢您的反馈。 +1
【解决方案2】:

我已经启动了 sshagent 并且它正在运行。

仅当您使用 SSH URL 时才相关。

当作为计划任务运行时,脚本在脚本的第二部分 (git push) 中终止。

可能是因为它使用不同的帐户(例如系统帐户)运行,并且VICredentialStoreItem 不会获得与从命令行执行时相同的凭据(作为正确的用户)
这些凭据仅适用于远程 HTTPS URL。不是 SSH 的 (git@...)

【讨论】:

  • 我正在作为 SSH URL 运行。我做了 git remote -v 来验证。此外,我使用同一个帐户来运行我用来手动运行脚本的计划任务。用于 VICredentialStore 的同一用户帐户
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多