[英]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.