Search code examples
gitpowershellgithubpowercli

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.


Solution

  • 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 [email protected]

    Script ran as a scheduled task without issue.

    It was the only way I could find persistence after reboots.