簡體   English   中英

來自 Windows 10 任務調度程序的 Powershell 腳本未啟動 Chrome

[英]Powershell script from Windows 10 task scheduler not launching Chrome

我制作了一個小的 Powershell 腳本“Start.ps1”,它在 Windows 啟動時啟動一些網站。 當我手動啟動它時,所有配置的網站都在 Chrome 中啟動。 但是,當我使用任務計划程序運行它時,任務會運行,但 Chrome 沒有啟動。

schtasks /create /tn "Start" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file 'C:\Users\myuser\Google Drive\Home\Start.ps1'"

我還在我的 powershell 腳本中添加了一些日志,所以我確定腳本已經啟動:

Function Write-Log {
  Param (
    [parameter(Mandatory=$True,HelpMessage='Log output')][string]$Log,
    [parameter(Mandatory=$True,HelpMessage='Log severity')][ValidateSet('Debug', 'Info', 'Warning', 'Error', 'Unknown')][string]$Severity,
    [parameter(Mandatory=$True,HelpMessage='Log message')][string]$Message
  )
  $Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
  $LocalScriptName = Split-Path -Path $myInvocation.ScriptName -Leaf
  If ( $Log -eq 'Verbose' ) {
    Write-Verbose -Message ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -eq 'Debug' ) {
    Write-Debug -Message ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -eq 'Output' ) {
    Write-Verbose -Message ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message)
  }
  ElseIf ( $Log -match '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(?::(?<port>\d+))$' -or $Log -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
    $IpOrHost = $log.Split(':')[0]
    $Port = $log.Split(':')[1]
    If ( $IpOrHost -match '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$' ) {
      $Ip = $IpOrHost
    }
    Else {
      $Ip = [Net.Dns]::GetHostAddresses($IpOrHost)[0].IPAddressToString
    }
    Try {
      $LocalHostname = ([Net.Dns]::GetHostByName((& "$env:windir\system32\hostname.exe")).HostName).tolower()
      $JsonObject = (New-Object -TypeName PSObject | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue logsource -InputObject $LocalHostname | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue hostname -InputObject $LocalHostname | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue scriptname -InputObject $LocalScriptName | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue logtime -InputObject $Now | 
        Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue severity_label -InputObject $Severity | 
      Add-Member -PassThru -NotePropertyName NoteProperty -NotePropertyValue message -InputObject $Message ) 
      If ( $psversiontable.psversion.major -ge 3 ) {
        $JsonString = $JsonObject | ConvertTo-Json
        $JsonString = $JsonString -replace "`n",' ' -replace "`r",' '
      }
      Else {
        $JsonString = $JsonObject | ConvertTo-Json2
      }               
      $Socket = New-Object -TypeName System.Net.Sockets.TCPClient -ArgumentList ($Ip,$Port) 
      $Stream = $Socket.GetStream() 
      $Writer = New-Object -TypeName System.IO.StreamWriter -ArgumentList ($Stream)
      $Writer.WriteLine($JsonString)
      $Writer.Flush()
      $Stream.Close()
      $Socket.Close()
    }
    Catch {
      Write-Verbose -Message ("{0}: {1}: Error: Something went wrong while trying to send message to logserver `"{2}`"." -f $Now, $LocalScriptName, $Log)
    }
    Write-Verbose -Message ('{0}: {1}: {2}: Ip: {3} Port: {4} JsonString: {5}' -f $Now, $LocalScriptName, $Severity, $Ip, $Port, $JsonString)
  }
  ElseIf ($Log -match '^((([a-zA-Z]:)|(\\{2}\w+)|(\\{2}(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}))(\\(\w[\w ]*))*)') {
    If (Test-Path -Path $Log -pathType container){
      Write-Host ('{0}: {1}: Error: Passed Path is a directory. Please provide a file.' -f $Now, $LocalScriptName)
      Exit 1
    }
    ElseIf (!(Test-Path -Path $Log)) {
      Try {
        $Null = New-Item -Path $Log -ItemType file -Force   
      } 
      Catch { 
        $Now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss,fff'
        Write-Host ("{0}: {1}: Error: Write-Log was unable to find or create the path `"{2}`". Please debug.." -f $Now, $LocalScriptName, $Log)
        exit 1
      }
    }
    Try {
      ('{0}: {1}: {2}: {3}' -f $Now, $LocalScriptName, $Severity, $Message) | Out-File -filepath $Log -Append   
    }
    Catch {
      Write-Host ("{0}: {1}: Error: Something went wrong while writing to file `"{2}`". It might be locked." -f $Now, $LocalScriptName, $Log)
    }
  }
}

$Logfile="c:\Tempo\Start.log"
Write-Log $Logfile Info "Initiating Chrome Site Startup"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://outsideit.net"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://psychologischezorgopmaat.be"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://github.com"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://gitlab.com"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "http://localhost:9200"
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "http://localhost:5601"
Write-Log $Logfile Info "Finalizing Chrome Site Startup"

如何在任務計划程序中完成這項工作? 我嘗試使用 SYSTEM 和我自己的用戶以及“以最高權限運行”選項運行它。

當您手動運行Scheduled Tasks時,始終測試計划任務是否按預期工作( Scheduled Tasks - 右鍵單擊​​您的任務並Run它)。 當它起作用時,繼續測試它在您啟動計算機時(或任何時候,取決於觸發器)是否按預期工作。

當我使用SYSTEM用戶帳戶通過 cmd 導入您的任務時,默認設置為“無論用戶是否登錄都運行”。 如果我嘗試手動運行它,腳本將無法啟動。 然后我將用戶更改為我的帳戶,將選項更改為“僅在用戶登錄時運行”,這為我解決了這個問題(之前也有很多次)。 使用SYSTEM用戶時,我無法更改此選項。

計划任務設置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM