#Param (
#    $DestinationServer,
#    [string]$Email
#)

$PSUJobId = $UAJob.Id

Set-JDEnvironment -Name Prod

$MaintenanceWindow=(Get-ICE-MaintenanceWindow -Credential $Secret:snow_vmcred)

if ($MaintenanceWindow.ScheduledThisMonth -eq $false) {
  Send-ICE-Mail -jobid $PSUJobId -Message "Maintence Window Failed Integraty Check" -Status ActionNeeded -recipientsString fjpeterson@nd.gov
}

$Day=$MaintenanceWindow

$Day=$MaintenanceWindow.DaysToChangeWindow 
"It is $Day days to Production Maintenance Window."

if ($MaintenanceWindow.DaysToChangeWindow -eq 10 -and $Hour -eq "18") { #6pm Thursday
    $Message="Deploying Cert Changes to Test" 
    Send-ICE-Mail -jobid $PSUJobId -message  $Message -recipientsString fjpeterson@nd.gov
    #Idealy we get the oneds that were applied this week have to use date of not before Feild!

    $DestinationServer=(Get-ICE-CertUpforDeploy|Where-Object {$_.Environment -eq "Test"}).DestinationServer
  } elseif  ($MaintenanceWindow.DaysToChangeWindow -eq 0 -and $Hour -eq "4") { #4am Sunday
    $Message="Deploying Cert Changes to Production" 
    Send-ICE-Mail -jobid $PSUJobId -message  $Message -recipientsString fjpeterson@nd.gov
    $DestinationServer=(Get-ICE-CertUpforDeploy|Where-Object {$_.Environment -eq "Production"}).DestinationServer
    $DestinationServer=$null #Disable prod for now!
} else {
  "Skipping - Not time for Production or Test Maintenance"
}

$Scriptblock = {
  $config="C:\ProgramData\ITD\ITD.Infra-Certificate-External.Client\Config\IISBindingConfig.json"
  
  if (Test-Path $config) {
       Update-CCIISCertificateBinding 6>$null 2>&1
       $Configured=$true
  }

$config2="C:\ProgramData\ITD\ITD.Infra-Certificate-External.Client\Config\IISBindingConfig2.json"
  if (Test-Path $config2) {
        Invoke-CCIISCertificateUpdateFromConfig  6>$null 2>&1
        $Configured=$true
  }

If (!$Configured) {
    $hn=(hostname)
  "Skipped $hn - Not Configured"
  }  
}

ForEach ($ComputerName in $DestinationServer ) {  
  $results=Invoke-Command -ComputerName $ComputerName -Credential $Secret:ndgov_svcitdpsuwin -ScriptBlock $Scriptblock
  if ($results -eq $null) {
    Send-ICE-Mail -jobid $PSUJobId -message "$ComputerName Binding Not Configured, Will not update website" -recipientsString fjpeterson@nd.gov
  }  
}
