AutoGRAPH.NET Service Examples Powershell

Материал из TK
Перейти к: навигация, поиск

Powershell_Basic.ps1

Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV

$URL = "http://m.tk-chel.ru/ServiceJSON";
""
Remove-Item "*.csv" -ErrorAction SilentlyContinue

"== Prepare and Login ========================================================================"
$Response = Invoke-WebRequest -URI http://m.tk-chel.ru/ServiceJSON
$loginParms = @{
    UserName = "demo"
    Password="demo"
}
$token = (Invoke-WebRequest -Uri "$URL/Login" -Method POST -Body $loginParms).Content
"Token: $token"
$token > "PS_Token.csv"
$headers = @{ "AG-Token" = $token }

"== EnumSchemas =============================================================================="
$schemas = (Invoke-WebRequest -Uri "$URL/EnumSchemas" -Headers $headers).Content | ConvertFrom-Json
foreach($x in $schemas) { $x.ID+": "+$x.Name }
$schemas | Export-Csv "PS_Schemas.csv" -NoTypeInformation

"== EnumDevices =============================================================================="
$SCHEMA = $schemas[0].ID
$devices = (Invoke-WebRequest -Uri "$URL/EnumDevices?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
"Groups: "+$devices.Groups.Length
"Devices: "+$devices.Items.Length
$devices.Groups | Select-Object ID,ParentID,Name | Export-Csv "PS_Devices_Groups.csv" -NoTypeInformation
$devices.Items | Select-Object ID,ParentID,Name,Allowed,Serial | Export-Csv "PS_Devices_Devices.csv" -NoTypeInformation
""

"== EnumGeofences ============================================================================"
$SCHEMA = $schemas[0].ID
$geofences = (Invoke-WebRequest -Uri "$URL/EnumGeofences?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
"Groups: "+$geofences.Groups.Length
"Devices: "+$geofences.Items.Length
$geofences.Groups | Select-Object ID,ParentID,Name | Export-Csv "PS_Geofences_Groups.csv" -NoTypeInformation
$geofences.Items | Select-Object ID,ParentID,Name | Export-Csv "PS_Geofences_Geofences.csv" -NoTypeInformation
""

"== GetOnlineInfoAll ============================================================="
$SCHEMA = $schemas[0].ID
$oi = (Invoke-WebRequest -Uri "$URL/GetOnlineInfoAll?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
"OnlineInfoItems: "+$oi.Length

$oiresult =@()
$dt = (Get-Date).ToUniversalTime()

foreach($item in $oi)
{
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "ID" -Value $item.Key
    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $item.Value.Name
    if($item.Value)
    {        
        $obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value $item.Value.DT
        $obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value $item.Value.LastPosition.Lat
        $obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value $item.Value.LastPosition.Lng
        $obj | Add-Member -MemberType NoteProperty -Name "State" -Value $item.Value.State
        $obj | Add-Member -MemberType NoteProperty -Name "Latency" -Value $dt.Subtract($item.Value.DT).TotalMinutes
    }
    else
    {
        $obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value ""
        $obj | Add-Member -MemberType NoteProperty -Name "Speed" -Value ""
        $obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value ""
        $obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value ""
        $obj | Add-Member -MemberType NoteProperty -Name "State" -Value ""
        $obj | Add-Member -MemberType NoteProperty -Name "Latency" -Value ""
    }
    $oiresult += $obj
}
$oiresult | Export-Csv "PS_GetOnlineInfoAll.csv" -NoTypeInformation