[英]Create A Task Using Powershell
下午好:
我寫了一個可以在本地機器上創建任務的PS腳本; 但是,我打算讓這個在列表中的多台機器上配置一個任務。 我似乎無法找到如何做到這一點的方法。 我確實看到了一些關於使用 New-CimSession 的內容,但我認為這不是我要尋找的......
這是我的腳本:
foreach ($confighost in (Get-Content -Path "C:\Users\*User*\Documents\Test.txt"))
{
#Check for existent script in Task Scheduler
$taskpath = "\\$confighost\c$\Windows\System32\Tasks\LocalUserCleanUp"
$testtask = Test-Path -path $taskpath
If ($testtask -eq $true)
{
#If Task is already present, Do Not Run
Write-Host "CleanUp Task Listed in Task Scheduler On" $confighost ". Will not Implement Task..."
}
else
{
#If Task Does Not Exist Create Task
New-CimSession -ComputerName $confighost
Write-Host "No CleanUp Task Listed in Task Scheduler, Creating Task..."
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "C:\Users\*Users*\Documents\ProfileCleanup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
$settings = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $settings -TaskName "LocalUserCleanUp" -Description "Checks user accounts nightly to see if they are older than 30 days"
}
}
我期待你的洞察力。 再次感謝您的幫助!
您可以使用Invoke-Command
在目標服務器上執行您的任務創建代碼:
$cred = Get-Credential # There are a number of methods to automate reading the credential from a file
Invoke-Command -Credential $cred -ComputerName server01, server02, server0X {
# Your task creation code here
}
如果您在本地會話中定義了任何需要在遠程代碼中可用的變量,您可以在您的Invoke-Command
ScriptBlock
引用它們,通過在本地變量前加上$using:
using
從using
范圍引用變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.