Skip to content

Instantly share code, notes, and snippets.

@j1n6
Last active August 29, 2022 09:37
Show Gist options
  • Save j1n6/f3007f880126e29b2f706c367aed3f92 to your computer and use it in GitHub Desktop.
Save j1n6/f3007f880126e29b2f706c367aed3f92 to your computer and use it in GitHub Desktop.
Powershell to sync and push to remote git repository via Windows Scheduled Tasks
' Hack to workaround the Powershell Console popup running on a Windows Scheduled Task
' Powershell limitations: https://github.com/PowerShell/PowerShell/issues/3028
'
' Add this file in the same git root directory as the sync.ps1
'
Dim shell,command
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim sScriptDir : sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
command = "powershell.exe -nologo -File " & sScriptDir & "\sync.ps1"
Set shell = CreateObject("WScript.Shell")
shell.Run command,0
function GitCommitAndPush {
$dateString = Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
git add .
git commit -q -m "$($dateString)"
git push -q
}
function GitPull {
git pull
}
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Set-Location -Path $scriptPath
GitPull
if(git status --porcelain |Where {$_ -match '^\?\?'}){
# untracked files exist
GitCommitAndPush
Exit 0
}
elseif(git status --porcelain |Where {$_ -notmatch '^\?\?'}) {
# uncommitted changes
GitCommitAndPush
Exit 0
}
else {
# tree is clean
Exit 0
}
@j1n6
Copy link
Author

j1n6 commented Oct 13, 2020

  • Put both files in the Git repo root directory as it will pick up as default path.
  • Generate ssh keys for push git repo without prompt password
  • Add as a scheduled task in windows scheduler to run the noop_sync.vbs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment