<#
   STEAMWARE - Rottoli GCarlo
   Mod. 2019-02

.DESCRIPTION

   Stoppa e disabilita Windows Update

#>

# Remove-Variable * -ErrorAction SilentlyContinue  # pulisco variabili x ISE

# Set-ExecutionPolicy Unrestricted   # per attivare l'esecuzione degli script

cls

# ----------------------------
# parametri per log errori
# ----------------------------
# path dello script ps1 mi serve a causa di problemi con lo schedulatore 
if ( $MyInvocation.MyCommand.Path -ne $null ) { 
    # $scriptPath = split-path -parent $MyInvocation.MyCommand.Path   
    # file di Log
    $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition  # path dello script ps1 mi serve a causa di problemi con lo schedulatore
    $LogFile    = (split-path -Leaf $MyInvocation.MyCommand.Definition).Replace('.ps1','_Log.txt')
    $ErrlogFile = (split-path -Leaf $MyInvocation.MyCommand.Definition).Replace('.ps1','_ErrorLog.txt')
    }
else {  
    CD
    $scriptPath = "C:\Steamware"
    $ErrlogFile = "DisableWU_ErrorLog.txt"       # "$scriptPath\ErrLog_Log.txt"   # se non lo voglio mettere  = $null quindi posso usare una variabile 
    $LogFile    = "DisableWU_log.txt"
}

$Date      = (Get-Date).ToString("dd/MM/yyyy HH:mm:ss")
$FileCheck = $LogFile.Replace('_Log.txt','_Check_Log.txt')

# attivo il Transcript
Start-Transcript -path "$scriptPath\$LogFile" | Out-Null

$Date      
# $scriptPath
$ErrlogFile
$LogFile

"-->> Eseguo Check Window Update"   
$ServiceStatus = get-service -DisplayName 'Windows update' | SELECT DisplayName, Status, StartType
$ServiceStatus | Format-Table -HideTableHeaders

$Date >> "$scriptPath\$FileCheck"
$ServiceStatus | Format-Table -HideTableHeaders >> "$scriptPath\$FileCheck"

# ----------------------------
# ----       MAIN       ------
# ----------------------------
TRY
{

    if ($ServiceStatus.Status -ine "Stopped" -or $ServiceStatus.StartType -ine "Disabled")  # riavvio il servizio
    { 
      "-->> Eseguo Stop e Disabled Window Update"

     get-service -DisplayName 'Windows update' | Stop-Service -ErrorAction Continue

     get-service -DisplayName 'Windows update' | Set-Service -startuptype "disabled"

     get-service -DisplayName 'Windows update' | SELECT DisplayName, Status, StartType   | Out-Host

    }
    else
    {
      "-->> Servizio Window Update già disabilitato"
    }

}  # end TRY

# ==== GESTIONE ERRORI =====
CATCH [Exception] 
{

    # salvo errore 
    "CATCH Error at $((Get-Date).ToString("dd/MM/yyyy HH:mm:ss"))" >> "$scriptPath\$ErrlogFile"

    $error[0]
    Write-Output $Error[0] >>  "$scriptPath\$ErrlogFile"

    Write-Output '>> List Exception'
    $err = $_.Exception
    while ( $err.InnerException ) 
    {
        $err = $err.InnerException
        Write-Output $err.Message # anche a video
        Write-Output $err.Message >>  "$scriptPath\$ErrlogFile"
    }

}
# ==== STEP FINALE - sempre eseguito =====
FINALLY
{
   "END execute at : $((Get-Date).ToString("dd/MM/yyyy HH:mm:ss"))" | Out-Host
  
   # Stop capturing output
   Stop-Transcript
}



