This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
windows.ps1
34 lines (27 loc) · 1.8 KB
/
windows.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ErrorActionPreference = "Stop"
$wd = $env:AZ_BATCH_TASK_WORKING_DIR
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco install -y python --version 3.6.3
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Write-Host "Current path: $env:Path"
Write-Host "Python version:"
python --version
pip install psutil python-dateutil applicationinsights==0.11.3
Write-Host "Downloading nodestats.py"
Invoke-WebRequest https://raw.githubusercontent.com/Azure/batch-insights/master/nodestats.py -OutFile nodestats.py
# Delete if exists
$exists = Get-ScheduledTask | Where-Object {$_.TaskName -like "batchappinsights" };
if($exists)
{
Write-Host "Scheduled task already exists. Removing it and restarting it";
Stop-ScheduledTask -TaskName "batchappinsights";
Unregister-ScheduledTask -Confirm:$false -TaskName "batchappinsights";
}
$pythonPath = get-command python | Select-OBject -ExpandProperty Definition
Write-Host "Resolved python path to $pythonPath"
Write-Host "Starting App insights background process in $wd"
$action = New-ScheduledTaskAction -WorkingDirectory $wd -Execute 'Powershell.exe' -Argument "Start-Process $pythonPath -ArgumentList ('.\nodestats.py','$env:AZ_BATCH_POOL_ID', '$env:AZ_BATCH_NODE_ID', '$env:APP_INSIGHTS_INSTRUMENTATION_KEY') -RedirectStandardOutput .\batch-insights.log -RedirectStandardError .\batch-insights.err.log -NoNewWindow"
$principal = New-ScheduledTaskPrincipal -UserID 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount -RunLevel Highest ;
Register-ScheduledTask -Action $action -Principal $principal -TaskName "batchappinsights" -Force ;
Start-ScheduledTask -TaskName "batchappinsights";
Get-ScheduledTask -TaskName "batchappinsights";