简体   繁体   中英

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.

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.

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). I can run that in git-bash without issue as well.

#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.

Thanks.

First, I was running the ssh URL which I validated by checking the origin.

Ultimately, I determined that the script was failing at the git portion of the script. 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.

Then I performed the following steps:

  1. Generated new SSH Key
  2. Added the SSH Key to the Agent
  3. Added the key to GitHub
  4. Validate SSH is functional - 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.

The script dies in the 2nd part of the script (git push) when run as a scheduled task.

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)
And those credentials would apply only to a remote HTTPS URL. Not an SSH one ( git@... )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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