source

파워셸 스크립트를 백그라운드에서 분당 한 번 실행

ittop 2023. 9. 14. 23:37
반응형

파워셸 스크립트를 백그라운드에서 분당 한 번 실행

파워셸 스크립트를 1분에 한 번 백그라운드에서 실행했으면 합니다.창이 나타나지 않을 수 있습니다.제가 그걸 어떻게 합니까?

Windows 작업 스케줄러를 사용하여 다음과 같이 스크립트를 실행합니다.

powershell -File myScript.ps1 -WindowStyle Hidden

또한 특정 사용자 계정에서 실행되는 스크립트를 생성하며, 해당 사용자가 로그온되어 있을 만 생성되는 것은 아닙니다.그렇지 않으면 콘솔 창이 나타납니다.

작업이 시스템으로 실행되도록 예약합니다.아래 명령은 powershell 콘솔 창을 표시하지 않고 백그라운드에서 스크립트를 실행하도록 스케줄링합니다.

schtasks /create /tn myTask /tr "powershell -NoLogo -WindowStyle hidden -file myScript.ps1" /scminute /mo 1 /ru 시스템

/ruswitch를 사용하면 예약된 작업이 실행될 사용자 컨텍스트를 변경할 수 있습니다.

아마도 이 시나리오로 충분할 것입니다.PowerShell 실행 파일을 매 분마다 시작하는 것은 아닙니다(비싸지만).대신, 작업자 스크립트를 1분에 한 번 호출하는 추가 스크립트를 호출하여 한 번 시작합니다(또는 실제로 작업자 스크립트가 종료되거나 실패한 후 1분 후에 대기합니다).

시작 Invoke-MyScript.ps1을 만듭니다.

for(;;) {
 try {
  # invoke the worker script
  C:\ROM\_110106_022745\MyScript.ps1
 }
 catch {
  # do something with $_, log it, more likely
 }

 # wait for a minute
 Start-Sleep 60
}

Cmd(예: startup.bat 파일)에서 실행:

start /min powershell -WindowStyle Hidden -Command C:\ROM\_110106_022745\Invoke-MyScript.ps1

PowerShell 창이 잠시 나타나지만 다음으로 인해 최소화됩니다.start /min그리고 한 순간에 영원히 숨겨지게 됩니다창 자체가 아니라 작업 표시줄 아이콘만 표시됩니다.심한 정도로 오지는 않아요.

보이는 창이 전혀 없이 매 분마다 반복되는 스크립트를 실행하도록 백그라운드 파워셸 작업을 만들려면 powershell을 관리자로 실행한 다음Register-ScheduledJob스크립트를 실행하는 cmdlet.다음은 이를 실현하는 방법의 예입니다.

Register-ScheduledJob -Name 'SomeJobName' -FilePath 'path\to\your\ps1' -Trigger (New-JobTrigger -Once -At "9/28/2018 0am" -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([TimeSpan]::MaxValue))

이 작업을 수동으로 실행하려면(문제 해결을 위해)Get-ScheduledJob다음과 같은 cmdlet:

(Get-ScheduledJob -Name 'SomeJobName').StartJob()

저는 스크립트 실행을 비활성화한 IT 조직의 제약 하에 일하고 있습니다.그러므로 저는 온라인으로 제한됩니다.제가 사용한 것은 다음과 같습니다.

while ($true) {<insert command(s) here>;start-sleep 2}

저는 파워쉘을 처음 접하니까 피드백이 있으시면 부드럽게 말씀해주세요 ;-).

작업의 작업 스케줄러 속성에 있는 [일반] 탭에서 "사용자가 로그온했는지 여부 실행" 라디오 버튼을 선택하면 Windows 7(윈도우 7) 컴퓨터에 창이 표시되지 않습니다.

스크립트를 시간 간격에 따라 자동으로 실행하려면(윈도우 OS의 경우)

1)  Open Schedule tasks from control panel.
2)  Select Task Scheduler Library from left side window.
3)  select Create Task from right side window.
4)  Enter Name in General tab (any name).
5)  Open Triggers tab -> New
6)  Select interval as needed.Use Advanced settings for repeat task. 
7)  Open Actions tab ->New
8)  Copy/Paste following line in Program/script *

* Here D:\B.ps1 in code is path to my B.ps1 file

C:\Windows\system32\Windows PowerShell\v1.0\powershell.exe D:\B.ps1

9)  ok and apply changes
10) Done!  

이 대답들은 역사적인 것입니다!백그라운드 작업으로 powershell 스크립트를 실행하려면 스크립트를 보관하는 폴더 내의 CLI에서 start-job.\script를 실행하십시오.

#   Filecheck.ps1

#   Version 1.0

#   Use this to simply test for the existance of an input file... Servers.txt.

#   I want to then call another script if the input file exists where the

#   servers.txt is neded to the other script.

#


    $workpath=".\Server1\Restart_Test"

#   Created a functon that I could call as part of a loop.


    function Filecheck

    {
    if (test-path $workpath\servers.txt)

        {

        rename-item $workpath\servers.txt servers1.txt

        "Servers.txt exist... invoking an instance of your script agains the list of servers"


        Invoke-Expression .\your_Script.ps1

        }

    Else
        {

            "sleeping"

            Start-Sleep -Seconds 60
        }

     }

    Do
        {
        Filecheck

        $fred=0
        # Needed to set a variabe that I could check in the while loop.

        # Probably a better way but this was my way.

        }

        While( $fred -lt 1 )

언급URL : https://stackoverflow.com/questions/2071496/run-a-powershell-script-in-the-background-once-per-minute

반응형