Solved

Powershell - concatenated command will not run

Posted on 2014-11-20
11
194 Views
Last Modified: 2014-11-20
I am trying to string together a Schtasks command (for task scheduler) in powershell.

Through multiple variables, I successfully create the following:

$ScheduleAdd = "Schtasks /create /tn " + $userAlias +"-Covering-" + $identityAlias + " /sc ONCE /SD " + $single_StartDate + " /st 00:01 " + "/tr " + $([char]34) + "Powershell -command " + $addScript + $([char]34)
	Write-Host $ScheduleAdd

Open in new window


if I run "Write-Host $ScheduleAdd" after it in the script, it will successfully write to the console the exact command that can be copied and pasted into command prompt and run to create the scheduled task.

So I'm trying to figure out, why is it not actually adding the task to task scheduler when I set the variable above?

I can copy/paste it into the prompt and run it, and it adds, but if I don't copy paste the command, it will not add?

So Write-Host $ScheduleAdd will print out just like this:

Schtasks /create /tn TSmith-Covering-BJones /sc ONCE /SD 11-24-2014 /st 00:01 /tr "Powershell -command \\server\t$\schedules\Add-TSmith-Covering-BJones-11-24-2014.ps1"

It will print that out, but Powershell isn't executing it. I have to copy and paste it into the powershell window, and then it executes it, adding it to the scheduled tasks.
0
Comment
Question by:garryshape
[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
  • 7
  • 4
11 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40456578
Try invoke-expression
iex $ scheduleadd
0
 

Author Comment

by:garryshape
ID: 40456584
hmm doesn't appear to work. I am in a PSSession with Exchange but I don' think that's the problem.
other commands run fine

iex $ScheduleAdd

not sure
0
 

Author Comment

by:garryshape
ID: 40456595
I even tried invoke-expression -command $ScheduleAdd
also tried $string = $ScheduleAdd | Out-String
then iex $string

no luck.

haha it's so weird! I need help lol
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

Author Comment

by:garryshape
ID: 40456648
So I tried this:
$ScheduleAdd > "\\path\output.txt"

and that appears to be the same string that write-host displays into the text file.

Do I need to output to text file and then re-import for the command to run?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40456687
This worked for me without issue:
$ScheduleAdd = "Schtasks /create /tn TSmith-Covering-BJones /sc ONCE /SD 11-24-2014 /st 00:01 /tr ""Powershell -command \\server\t$\schedules\Add-TSmith-Covering-BJones-11-24-2014.ps1"""
iex $ScheduleAdd

Open in new window

0
 

Author Comment

by:garryshape
ID: 40456689
Well I think the challenge is my $ScheduleAdd = has to be written out based on variables, so when I have multiple variables throughout the line, it starts to throw a fit because of all the quotes.

I'll try it out again and try the double quotes per your successful method
0
 
LVL 29

Expert Comment

by:becraig
ID: 40456691
It should work with no problem once you correctly escape your quotes etc.


Let me know if it works.
0
 

Author Comment

by:garryshape
ID: 40456706
LOL ok, well, it's adding the schedules to my task scheduler, not the computer I have a pssession against. I'm not sure why. I'll try adding the computer name, not sure if it's going to pass through the variables.
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40456719
You should be able to do it remotely just by specifying the target computer.

Schtasks /s $computer

Above should work with all your other flags
0
 

Author Comment

by:garryshape
ID: 40456724
Ok so I can't invoke expression, that's for local computer.

tried invoke-command, didn't work. But invoke-command has the "-session $sessionname" option, and I added the session, but I can only call a .ps1 file with invoke-command, apparently, so now I have to put the scheduler command in a ps1 file. heh. trying it out
0
 

Author Comment

by:garryshape
ID: 40456729
Oh man awesome, the /S flag, had no idea, it worked. Thanks so much
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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