遠程大批量操作windows服務器的計劃任務

在工作過程中,經常需要大批量對某一批次機器進行計劃任務設置,可以通過組策略實現,也可以通過腳本操作。本文演示如何通過powershell腳本遠程大批量給若干臺機器設置週期重啓的操作。

#生成10到30的數組
$a=(10..30);

#初始化空的數組
$pcname=@();

#給數組賦值
$a | %{$pc = "shoa"+ $_; $pcname += "$pc"; };

$pcname | %{
    $computername = $_
        #先刪掉已有的psdrive,防止報錯
    Remove-PSDrive -Name "remotepcpath" -Confirm:$false;

        #生成新的psdrive,用於拷貝文件到目的服務器的指定位置
    New-PSDrive -name "remotepcpath" -PSProvider "FileSystem" -root "\\$computername\c$"
    Copy-Item -Path C:\Scripts -Destination remotepcpath: -Recurse

    #遠程執行創建計劃任務
    Invoke-Command -ComputerName $computername -ScriptBlock {
        $Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\Scripts\reboot.ps1"'
        $trigger = New-ScheduledTaskTrigger -Daily  -At 3am
        $settings = New-ScheduledTaskSettingsSet
        $task = New-ScheduledTask -Action $Action -Trigger $trigger -Settings $settings
        Register-ScheduledTask -TaskName 'reboot' -InputObject $task
    }   
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章