Powershell Set-ScheduledTaskCredential

# Set-ScheduledTaskCredential.ps1# Written by Bill Stewart (bstewart@iname.com)#requires -version 2<#.SYNOPSISSets the credentials for one or more scheduled tasks on a computer..DESCRIPTIONSets the credentials for one or more scheduled tasks on a computer..PARAMETER TaskNameOne or more scheduled task names. Wildcard values are not accepted. This parameter accepts pipeline input..PARAMETER TaskCredentialThe credentials for the scheduled task. If you don't specify this parameter, you will be prompted for credentials..PARAMETER ComputerNameThe computer name where the scheduled task(s) reside..PARAMETER ConnectionCredentialThe credentials to use when connecting to the computer..EXAMPLEPS C:\>Set-ScheduledTaskCredential "My Scheduled Task"This command will prompt for credentials and configure the specified task using those credentials..EXAMPLEPS C:\>Set-ScheduledTaskCredential "Task 1","Task 2" -ComputerName server1This command will prompt for credentials and configure the named scheduled tasks on the computer server1..EXAMPLEPS C:\>Set-ScheduledTaskCredential "Task 1","Task 2" -ComputerName server1This command will prompt for credentials and configure the named scheduled tasks on the computer server1..EXAMPLEPS C:\>Get-Content TaskNames.txt | Set-ScheduledTaskCredential -ConnectionCredential (Get-Credential)This command will set scheduled task credentials for all tasks named in the file TaskNames.txt. There will be two credential prompts. The first prompt is to specify credentials to connect to the Task Scheduler service, and the second prompt is to specify credentials to use for the scheduled tasks.#>[CmdletBinding(SupportsShouldProcess=$TRUE)]param( [parameter(Mandatory=$TRUE,ValueFromPipeline=$TRUE)] [String[]] $TaskName,#The credential for the schedule task [System.Management.Automation.PSCredential] $TaskCredential,#The remote computer name, by default it is local machine name [String] $ComputerName=$ENV:COMPUTERNAME,#The credential to connect to the remote computer [System.Management.Automation.PSCredential] $ConnectionCredential)begin { $PIPELINEINPUT = (-not $PSBOUNDPARAMETERS.ContainsKey("TaskName")) -and (-not $TaskName) $TASK_LOGON_PASSWORD = 1 $TASK_LOGON_S4U = 2 $TASK_UPDATE = 4 $MIN_SCHEDULER_VERSION = 0x00010002 # Try to create the TaskService object on the local computer; throw an error on failure try { $TaskService = new-object -comobject "Schedule.Service" } catch [System.Management.Automation.PSArgumentException] { throw $_ } # Assume $NULL for the schedule service connection parameters unless -ConnectionCredential used $userName = $domainName = $connectPwd = $NULL if ($ConnectionCredential) { # Get user name, domain name, and plain-text copy of password from PSCredential object $userName = $ConnectionCredential.UserName.Split("\")[1] $domainName = $ConnectionCredential.UserName.Split("\")[0] $connectPwd = $ConnectionCredential.GetNetworkCredential().Password } try { $TaskService.Connect($ComputerName, $userName, $domainName, $connectPwd) } catch [System.Management.Automation.MethodInvocationException] { write-error "Error connecting to '$ComputerName' - '$_'" exit } # Returns a 32-bit unsigned value as a version number (x.y, where x is the # most-significant 16 bits and y is the least-significant 16 bits). function convertto-versionstr([UInt32] $version) { $major = [Math]::Truncate($version / [Math]::Pow(2, 0x10)) -band 0xFFFF $minor = $version -band 0xFFFF "$($major).$($minor)" } if ($TaskService.HighestVersion -lt $MIN_SCHEDULER_VERSION) { write-error ("Schedule service on '$ComputerName' is version $($TaskService.HighestVersion) " + "($(convertto-versionstr($TaskService.HighestVersion))). The Schedule service must " + "be version $MIN_SCHEDULER_VERSION ($(convertto-versionstr $MIN_SCHEDULER_VERSION)) " + "or higher.") exit } # This prevents a scoping problem--if the $TaskCredential variable # doesn't exist, it won't get created in the correct scope--create # new variable as a workaround $NewTaskCredential = $TaskCredential if (-not $NewTaskCredential) { $NewTaskCredential = $HOST.UI.PromptForCredential("Task Credentials", "Please specify credentials for the scheduled task.", "", "") if (-not $NewTaskCredential) { write-error "You must specify credentials." exit } } function set-scheduledtaskcredential2($taskName) { $rootFolder = $TaskService.GetFolder("\") try { $taskDefinition = $rootFolder.GetTask($taskName).Definition } catch [System.Management.Automation.MethodInvocationException] { write-error "Scheduled task '$taskName' not found on '$computerName'." return } $logonType = $taskDefinition.Principal.LogonType # No need to set credentials for tasks that don't have stored credentials. if (-not (($logonType -eq $TASK_LOGON_PASSWORD) -or ($logonType -eq $TASK_LOGON_S4U))) { write-error "Scheduled task '$taskName' on '$ComputerName' doesn't have stored credentials." return } if (-not $PSCMDLET.ShouldProcess("Task '$taskName' on computer '$ComputerName'", "Set scheduled task credentials")) { return } try { [Void] $rootFolder.RegisterTaskDefinition($taskName, $taskDefinition, $TASK_UPDATE, $NewTaskCredential.UserName, $NewTaskCredential.GetNetworkCredential().Password, $logonType)Write-Host "Updated task '$taskName'" } catch [System.Management.Automation.MethodInvocationException] { write-error "Error updating scheduled task '$taskName' on '$computerName' - '$_'" } }}process { if ($PIPELINEINPUT) { set-scheduledtaskcredential2 $_ } else { $TaskName | foreach-object { set-scheduledtaskcredential2 $_ } }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值