Hi, I need a hint for a script that every day, for example, at 20 o’clock will ping and shut down all enabled computers from the domain.
So far I have something like this, admittedly I don’t know if it’s right, but I would still like to add to it this specific time at which the script should work.
//here are the lines with the ping that work and dump the result to a file
$s = Get-Content -Path ./Domain01.txt
$c = Get-Credential -Credential domain\Admin
Stop-Computer -ComputerName $s -Force -Credential $c
I’m not sure if I get what you mean with that. If you want to run this script at a certain time you should run it at a certain time. If you don’t want to run it manually you can use the task scheduler. I’d recommend to run this task already with the proper account. Then you don’t need to use it inside the script.
But 2 or 3 test computers to your ./Domain01.txt and test it! … I’d actually expect it to be .\Domain01.txt instead (with a backslash instead a slash) but since PowerShell is very forgiving it should workl this way as well.
I agree with Olaf, I would use the account with elevated access to run the task.
Here is a good starter template
#Choose your version of Powershell running the task, if you need to find your version you can open Powershell and type $PSVersionTable
$Posh7Dir = "C:\Program Files\PowerShell\7\pwsh.exe"
$Posh5Dir = "C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe"
$Action = New-ScheduledTaskAction -Execute $Posh7Dir -Argument "Stop-Computer -ComputerName (Get-Content -Path ./Domain01.txt) -Force"
$Trigger = New-ScheduledTaskTrigger -Daily -At #Put time here
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger
Register-ScheduledTask -TaskName "YourTaskName" -InputObject $Task