A
A
Artem Kiryanov2018-07-25 00:42:51
PowerShell
Artem Kiryanov, 2018-07-25 00:42:51

Installing a task via Powershell. How to do?

Good evening everyone. Faced a problem. You need to install the script on many PCs from your car via Powershell. What was done: I manually created a task on 1 PC and exported it to xml, then wrote the following command:
schtasks.exe /create /S ServerName /RU TEST\Ivanov /RP password /TN time_reload /XML D:\MyDisk\xml \nts_reload.xml
Actually this command works, but I need to specify a group of machines. I am running this command from Powershell.
WinRM - disabled due to security conditions (Invoke-Command) will not work.
Tell me what to write or how to rewrite? Or is it just easier to write a script like NewScheduledTaskAction and Register in Powershell?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
azarij, 2018-07-25
@hacker342

and so:
$server_list = "server1", "server2", "server3", "server4"
foreach ($server in $server_list) {
start-process -filepath schtasks.exe -argumentlist "/create /S $server /RU TEST \Ivanov /RP password /TN time_reload /XML D:\MyDisk\xml\nts_reload.xml"
}

C
chupasaurus, 2018-07-25
@chupasaurus

WinRM - disabled for security reasons
It remains only along, the Microsoft SSH server was never delivered.

S
Sergey Kovalev, 2019-10-18
@Sergey-S-Kovalev

I've been using this for years:

cls
# Правим кодировку, что бы читались руские символы в выводе консоли
[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")

# Задаем список ПК
$AllTheComputers = "PC1", "PC2", "PC3"

# Задаем команду для таскшедулера
$cmd = 'powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -file "\\server.domain.local\Scripts\copy-logs-to-fileserver.ps1"'

# Задаем имя создаваемого задания
$TaskName = "AdminTask - AnScheduledTask"

# Обходим все ПК по очереди
foreach ($computer in $AllTheComputers) {

Write-Host -ForegroundColor Green "Для компьютера"$computer "результат: " 

# Если нужно создать одноразовое задание 
SCHTASKS /Create  /S $computer /RU SYSTEM /F /SC ONCE /TN $TaskName /TR $cmd /ST 23:59:00 /RL HIGHEST

# Если нужно создать одноразовае задание под определенным пользователем и одноразово запустить
SCHTASKS /Create  /S $computer /RU [email protected] /RP PasswordHere!1 /F /SC ONCE /TN $TaskName /TR $cmd /ST 23:59:00 /RL HIGHEST

# Если нужно создать ежедневное задание выполняемое от имени системы
SCHTASKS /Create /S $computer /RU SYSTEM /SC DAILY /RI 30 /DU 23:59 /TN $TaskName /TR $cmd /ST 05:25:00 /sd 16/07/2018 /F /RL HIGHEST

# Если после создания нужно сразу запустить
Start-Sleep -Seconds 2
SCHTASKS /run /S $computer /TN $TaskName

# Удаление существующей задачи уже не требуется
SCHTASKS /Delete /S $computer /TN $TaskName /F
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question