<# Scheduled Task metadata
General
    SyncVMwareSharePointTags
    run as ndgov\svcitdiaassched
        run whether user is logged on or not
        run with highest privileges
 Triggers
    Daily, 10:15am, repeat every 8 hours
Actions
    powershell.exe -file "D:\PowerShellTasks\SetVMTagAndMoveToAppNameFolder\SyncVMwareTagsFromSharePoint.ps1"
    "C:\Program Files\PowerShell\7\pwsh.exe" -file "F:\SyncVMwareTagsFromSharePoint\SyncVMwareTagsFromSharePointTask.ps1"
    
Settings
    allow task to be run on demand
    stop the task if it runs longer than 3 days
    if the running task does not end when requested, force it to stop
#>

$TimeStamp = Get-Date -UFormat "%Y%m%d-%H%M%S"
$TranscriptPath = "F:\SyncVMwareTagsFromSharePoint\Logs\SyncVMwareTagsFromSharePointTask-$Timestamp.log"
Start-Transcript $TranscriptPath



try {
    #Sync-ITDVMwareSharePointTags -vCenterCredential $svcitdvmvcauto -SharePointCredential $svcitdiaassprw -Verbose

    Connect-ITDvCenter -Credential $svcitdvmvcauto
    Sync-ITDVMwareSharePointTags -SharePointCredential $svcitdiaassprw -Verbose
    Disconnect-VIServer -Server * -Confirm:$false
}
catch {
    Send-MailMessage -To itdvctremailalerts@nd.gov -From itdvmvc1script@nd.gov -Subject "Task SyncVMwareTagsFromSharePoint errored" -Body "Task started $TimeStamp.`nCheck logs for further information" -SmtpServer apprelay1.nd.gov
}