Solved

Schtasks Passing variable

Posted on 2013-10-28
2
616 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 79

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 68

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

864 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

25 Experts available now in Live!

Get 1:1 Help Now