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
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]
Please help me on resolving this.