0

So, I'm trying to create a scheduled task on a large number of servers (60+) that will run a PowerShell script. I have the schtask command figured out and tested, and if I run it against a single server, it runs just fine.

So, since I need to do it against 60+ servers, I created a .txt file containing all of the site codes. Then I have a variable created, defined as that text file. Then the foreach-object loop is created, starting with defining a variable adding the site code to the rest of the server name, then running the schtasks command against that variable. And that I'm guessing is where it's failing (it's just saying "Supply values for the following parameters", so it's not super helpful.

Does anyone have any idea as to where I'm going wrong? I'm not the strongest scripter, so any help would be super appreciated. Here's the full script:

 $Site = Get-Content -Path D:\folder1\Sites.txt | ForEach-Object { $server = $site + "-file-p50" .\schtasks /Create /S $Server /RU "SYSTEM" /RP /SC ONCE /TN "DFS Mapping" /TR "powershell.exe C:\users\<username>\desktop\datapaths.ps1" /ST "4:00" /SD "06/06/2017" /F /RL HIGHEST } 

1 Answer 1

3

Your $site variable is currently an array of sites. It looks like you may be meaning to have this line inside your ForEach-Object scriptblock

$server = $_ + "-file-p50" 

$_ is the token for the current item in the ForEach-Object loop iteration.

1
  • Yep, my co-worker (our internal scripting Lord and Master) finally finished something he'd been working on for hours RIGHT after I posted this, so I was able to ask him about it. He basically said the same thing, so I (he) changed it, we tested it, and it seems to have done the trick. Thanks though! Commented Jun 5, 2017 at 19:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.