[英]PowerShell git push as a scheduled task
我們為Win Server 2016設置了用於自動報告腳本的功能,以在將其作為計划任務執行時將報告推送到github。
以代理用戶身份登錄時,我可以運行該腳本而不會出現問題。 我已經開始了共享,它正在運行。 作為計划任務運行時,該腳本在腳本的第二部分(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的計划任務。
謝謝。
首先,我運行了ssh URL,並通過檢查源進行了驗證。
最終,我確定腳本在腳本的git部分失敗。 當我嘗試以各種方式啟動代理並將密鑰添加到代理時,它仍然無效。 我發現以下方法可以持續工作(即使在重新啟動后)。
我從https://github.com/PowerShell/Win32-OpenSSH/releases安裝了OpenSSH下載文件,將其解壓縮到一個文件夾,然后運行腳本:install-sshd.ps1,該腳本將安裝2個OpenSSH服務。
然后,我執行了以下步驟:
腳本作為計划任務運行而沒有問題。
這是重啟后才能找到持久性的唯一方法。
我已經開始了共享,它正在運行。
僅在使用SSH URL時才有意義。
作為計划任務運行時,該腳本在腳本的第二部分(git push)中消失。
可能是因為它使用不同的帳戶(例如系統帳戶)運行,並且VICredentialStoreItem
不會獲得與從命令行(作為正確的用戶)執行時獲得的相同的憑據。
這些憑據僅適用於遠程HTTPS URL。 不是SSH的( git@...
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.