Solved

Schtasks Passing variable

Posted on 2013-10-28
2
640 Views
Last Modified: 2013-11-05
Hello All,

 I am in a situation where I need to pass the argument to schtask to change the scheduled task time on different remote machine. I have below powershell script which does good job when I put the ST manually in the schtasks

$Servers = Get-Content C:\Servers.txt
foreach($Server in $Servers)
{
$Time = Read-host "Enter Time"
[string]$command = 'echo <Password> | schtasks /change /TN "PatchManually" /ST [b]07:00'[/b]
[string]$cmd = "CMD.EXE /C " +$command
if((Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $Server).ReturnValue -eq 0)
{
Write-Host "$Server" scheduled task timing changed successfully
}
else
{
Write-Host "$Server" has some problem , please change the scheduled task manually.
}
}

but can I change 07:00AM and pass it to a variable like below? (It appears it wont work for me)

$time = Read-Host "Enter the time which needs to be put on scheduled task"
[string]$command = 'echo <Password> | schtasks /change /TN "PatchManually" /ST [b]$time[/b]

Open in new window


Please help me on resolving this.

Thanks,

-Prashant Girennavar.
0
Comment
Question by:Prashant Girennavar
2 Comments
 
LVL 80

Accepted Solution

by:
David Johnson, CD, MVP earned 400 total points
ID: 39607445
$Servers = Get-Content C:\scripts\Servers.txt
foreach($Server in $Servers)
{
$Time = Read-host "Enter Time"
[string]$command = 'echo <Password> | schtasks /change /TN "PatchManually /ST ' + $time
[string]$cmd = "CMD.EXE /C " +$command
if((Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $Server).ReturnValue -eq 0)
{
Write-Host "$Server" scheduled task timing changed successfully
}
else
{
Write-Host "$Server" has some problem , please change the scheduled task manually.
}
}

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 39608059
Any reason you don't use the /S (system) parameter for schtasks directly instead of issueing a remote cmd.exe per WMI?

I assume we cannot assume you use PowerShell 3 (has direct access to scheduled tasks)?

Anyway, the point with /ST option of schtasks is that you need to provide military time (24 hours), so "07:00am" will not work. If you remember that, you can use the time string the same way you did with '07:00'.

BTW, if using ve3ofa's code, remove the double quote in line 5.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Single quotes and tick quotes within strings. 9 26
AWK: Pythagoras script 6 23
PowerShell logging 1 16
Where to download python requests library 6 31
Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question