?
Solved

Schtasks Passing variable

Posted on 2013-10-28
2
Medium Priority
?
728 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 84

Accepted Solution

by:
David Johnson, CD, MVP earned 1600 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 71

Assisted Solution

by:Qlemo
Qlemo earned 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 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