Home > Software design >  how can i 'instance' Register-ScheduledTask and modify it later?
how can i 'instance' Register-ScheduledTask and modify it later?

Time:10-18

this is my very first powershell script.
there are some specific things that i know could be different, i just dont know how to do it.

like the Set-ScheduledTask repeated 3 times in the switch, for example.
i whish i knew how to 'instance' the task object outside the switch and add/modify the properties (parameters?) inside, calling it to execute on the last line of the script.

well, any help is greatly appreciated.

#Requires -RunAsAdministrator

# script parameters
[CmdletBinding(SupportsShouldProcess)]
param (
    [ValidateSet('askme', 'hardcoded', 'system')]
    [string]$taskrunner = 'system'
)

# print extra info on screen
$VerbosePreference = "Continue"

# clear screen
Clear-Host

# i always work from the script directory
Set-Location $PSScriptRoot

"
---
Running $($MyInvocation.Line)
---
** description in portuguese about what the script is suposed to do **

Este script importa um xml (previamente exportado do agendador de tarefas)
e agenda esta tarefa simulando um servico, dispensando o logon do usuario.
---
"

$taskname = 'sync-owncloud-as-a-service'
$taskxml = (Get-Content "$taskname.xml" | Out-String)

Register-ScheduledTask -Force -TaskName $taskname -xml $taskxml

switch ($taskrunner) {
    'askme' {  
        $credential = Get-Credential -Message 'account credentials to run the task' -Title '--- ATENCAO ---'
        $taskuser = $credential.UserName
        $taskpassword = $credential.GetNetworkCredential().Password
        Set-ScheduledTask -TaskName $taskname -User $taskuser -Password $taskpassword
    }
    'hardcoded' {  
        $taskuser = 'corp\nonprivilegeduser'
        $taskpassword = 'verystrongpassaword'
        Set-ScheduledTask -TaskName $taskname -User $taskuser -Password $taskpassword
    }
    'system' {  
        $taskprincipal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
        Set-ScheduledTask -TaskName $taskname -Principal $taskprincipal
    }
    Default {
        '** how do i get here? **'
        Write-Error "Parameter $_ does not apply to this script"
    }
}

# print task info (check if its ok)
Get-ScheduledTaskInfo $taskname

Edit: some terms may sound wrong since i'm not a native english speaker. if i'm not making sense, please feel free to edit my question or ask me to rephrase it.

CodePudding user response:

Congrats on your first script. What you're possibly looking for here is splatting.

# initialize parameters for Set-ScheduledTask
$params = @{
    TaskName = $taskname
}

# configure parameters
switch ($taskrunner) {
    'askme' {  
        $credential = Get-Credential -Message 'account credentials to run the task' -Title '--- ATENCAO ---'
        $params["User"] = $credential.UserName
        $params["Password"] = $credential.GetNetworkCredential().Password
    }
    'hardcoded' {  
        $params["User"] = 'corp\nonprivilegeduser'
        $params["Password"] = 'verystrongpassaword'
    }
    'system' {  
        $params["Principal"] = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
    }
    Default {
        throw "Parameter $_ does not apply to this script"
    }
}

# call Set-ScheduledTask once with the configred parameters
Set-ScheduledTask @params
  • Related