简体   繁体   English

PowerShell git push作为计划任务

[英]PowerShell git push as a scheduled task

We have a Win Server 2016 set up for automated reporting scripts to push the reports up to github when executed as a scheduled task. 我们为Win Server 2016设置了用于自动报告脚本的功能,以在将其作为计划任务执行时将报告推送到github。

I can run this script without issue when I'm logged in as the proxy user. 以代理用户身份登录时,我可以运行该脚本而不会出现问题。 I've started the sshagent and it's running. 我已经开始了共享,它正在运行。 The script dies in the 2nd part of the script (git push) when run as a scheduled task. 作为计划任务运行时,该脚本在脚本的第二部分(git push)中消失。

I've tried running the git-push portion separately as a scheduled task and I still can't get it to run (ssh agent is still running). 我尝试将git-push部分作为计划任务单独运行,但仍然无法运行(ssh代理仍在运行)。 I can run that in git-bash without issue as well. 我也可以在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

If I can't get this running in PowerShell, I'd be happy just to have a scheduled task that runs in POSH or git bash that will do the git push. 如果无法在PowerShell中运行此程序,那么我很高兴能在POSH或git bash中运行将执行git push的计划任务。

Thanks. 谢谢。

First, I was running the ssh URL which I validated by checking the origin. 首先,我运行了ssh URL,并通过检查源进行了验证。

Ultimately, I determined that the script was failing at the git portion of the script. 最终,我确定脚本在脚本的git部分失败。 While I had tried starting the agent and adding the key to the agent in a variety of ways, it still died. 当我尝试以各种方式启动代理并将密钥添加到代理时,它仍然无效。 I found the following method to work consistently (even after reboots). 我发现以下方法可以持续工作(即使在重新启动后)。

I installed the OpenSSH download from https://github.com/PowerShell/Win32-OpenSSH/releases , unzipping to the a folder and then running the script: install-sshd.ps1 which installs 2 OpenSSH services. 我从https://github.com/PowerShell/Win32-OpenSSH/releases安装了OpenSSH下载文件,将其解压缩到一个文件夹,然后运行脚本:install-sshd.ps1,该脚本将安装2个OpenSSH服务。

Then I performed the following steps: 然后,我执行了以下步骤:

  1. Generated new SSH Key 生成新的SSH密钥
  2. Added the SSH Key to the Agent 将SSH密钥添加到代理
  3. Added the key to GitHub 将密钥添加到GitHub
  4. Validate SSH is functional - ssh git@github.mycompany.com 验证SSH是否正常-ssh git@github.mycompany.com

Script ran as a scheduled task without issue. 脚本作为计划任务运行而没有问题。

It was the only way I could find persistence after reboots. 这是重启后才能找到持久性的唯一方法。

I've started the sshagent and it's running. 我已经开始了共享,它正在运行。

That is relevant only if you are using an SSH URL. 仅在使用SSH URL时才有意义。

The script dies in the 2nd part of the script (git push) when run as a scheduled task. 作为计划任务运行时,该脚本在脚本的第二部分(git push)中消失。

Probably because it is running with a different account (like the System one) and VICredentialStoreItem won't get the same credentials as the ones when executed from command line (as the right user) 可能是因为它使用不同的帐户(例如系统帐户)运行,并且VICredentialStoreItem不会获得与从命令行(作为正确的用户)执行时获得的相同的凭据。
And those credentials would apply only to a remote HTTPS URL. 这些凭据仅适用于远程HTTPS URL。 Not an SSH one ( git@... ) 不是SSH的( git@...

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

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