Search code examples
powershellpowershell-2.0powershell-3.0powershell-4.0

Powershell IF Statement - Enable-ScheduledTask


Our current environment is so .. we have a gold image an numerous citrix machines. Everyonce in awhile we have to provision these gold image. We have on our gold image some scheduled tasks (deactivated on the gold image), and then when after the provisioning they are enabled only on a specific server, we have a Scheduled task to enable other tasks that we want on that specific machine.

Code so far:

$HostName = {(Get-CIMInstance CIM_ComputerSystem).Name}
$ServerName = "HOST01234"

If (& $HostName -eq $ServerName) {
Enable-ScheduledTask -TaskName "1"
Enable-ScheduledTask -TaskName "2"
Enable-ScheduledTask -TaskName "3"
Enable-ScheduledTask -TaskName "4"
Enable-ScheduledTask -TaskName "5"
} else {
exit
}

Practically one variable checks for the current server hostname, and then runs the if else statement to make sure the script is on the specific server. However when running this command - doesnt matter what server I am on, the enable-scheduletask commands are executed.


Solution

  • try this (I removed the curly brackets from the $Hostname and & in your if statement):

    $HostName = (Get-CIMInstance CIM_ComputerSystem).Name
    $ServerName = "HOST01234"
    
    If ($HostName -eq $ServerName) {
    Enable-ScheduledTask -TaskName "1"
    Enable-ScheduledTask -TaskName "2"
    Enable-ScheduledTask -TaskName "3"
    Enable-ScheduledTask -TaskName "4"
    Enable-ScheduledTask -TaskName "5"
    } else {
    exit
    }