可以使用Windows任务计划程序来设置应用程序在启动时自动运行。以下代码示例展示了如何使用Graph API创建任务计划程序。首先,需要获取访问令牌,并使用此令牌调用Graph API来创建任务计划程序。
# 获取访问令牌
$tenantId = "yourTenantId"
$appId = "yourAppId"
$secret = "yourAppSecret"
$clientId = "$appId@$tenantId"
$body = @{
    client_id     = $clientId
    client_secret = $secret
    grant_type    = "client_credentials"
    scope         = "https://graph.microsoft.com/.default"
}
$response = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" `
    -Method POST -Body $body
$accessToken = $response.access_token
# 创建任务计划程序
$header = @{
    Authorization = "Bearer $accessToken"
    'Content-Type' = 'application/json'
}
$taskName = "MyTask"
$appPath = "C:\Program Files\MyApp\MyApp.exe"
$triggerStartTime = "2022-01-01T08:00:00"
$intervalMinutes = 60
$body = @{
    definition =
    @{
        trigger =
        @{
            schedule = @{
                monthly = @{
                    dayOfMonth = 1
                    startTime = $triggerStartTime
                    interval = @{
                        minutes = $intervalMinutes
                    }
                }
            },
            type = "Calendar"
        },
        actions = @{
            executableActions = @{
                executablePath = $appPath
            }
        },
        settings = @{
            disallowStartIfOnBatteries = $false
            startWhenAvailable = $true
            priority = 7
            executionTimeLimit = "P3D"
        }
    }
} | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/deviceManagement/groupPolicyConfigurations" `
    -Method POST -Body $body -Headers $header
此代码示例使用了设备管理API,因此需要相应