Solved

Schtasks Passing variable

Posted on 2013-10-28
2
606 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 78

Accepted Solution

by:
David Johnson, CD, MVP earned 400 total points
Comment Utility
$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 68

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now