?
Solved

Schtasks Passing variable

Posted on 2013-10-28
2
Medium Priority
?
688 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

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 70

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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