AutoGRAPH.NET Service Examples Powershell — различия между версиями

Материал из TK
Перейти к: навигация, поиск
(Новая страница: «== Powershell_Basic.ps1 == Нижприведенный файл выполняет несколько запросов и сохраняет их результ…»)
 
Строка 2: Строка 2:
   
 
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV
 
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV
  +
<pre>$URL = "http://m.tk-chel.ru:8300";
 
<pre>
 
$URL = "http://m.tk-chel.ru:8300";
 
 
$SCHEMA = "DemoCEBIT"
 
$SCHEMA = "DemoCEBIT"
 
""
 
""
Строка 12: Строка 10:
 
$c = New-Object -TypeName System.Net.WebClient;
 
$c = New-Object -TypeName System.Net.WebClient;
 
$c.Headers.Add("Content-Type", "text/json");
 
$c.Headers.Add("Content-Type", "text/json");
$token = $c.UploadString("$URL/Login", "{""UserName"":""service-example"",""Password"":""12345678""}").Trim("""");
+
$token = $c.UploadString("$URL/Login", "{""UserName"":""demo"",""Password"":""demo""}").Trim("""");
 
$c.Headers.Add("AG-TOKEN", $token)
 
$c.Headers.Add("AG-TOKEN", $token)
 
"Token: $token"
 
"Token: $token"

Версия 09:31, 30 марта 2019

Powershell_Basic.ps1

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

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

"== Prepare and Login ========================================================================"
$c = New-Object -TypeName System.Net.WebClient;
$c.Headers.Add("Content-Type", "text/json");
$token = $c.UploadString("$URL/Login", "{""UserName"":""demo"",""Password"":""demo""}").Trim("""");
$c.Headers.Add("AG-TOKEN", $token)
"Token: $token"
$token > "PS_Token.csv"
""

"== EnumSchemas =============================================================================="
$schemas = $c.DownloadString("$URL/EnumSchemas") | ConvertFrom-Json
foreach($x in $schemas) { $x.ID+": "+$x.Name }
$schemas | Export-Csv "PS_Schemas.csv" -NoTypeInformation
""

"== EnumDevices =============================================================================="
$devices = $c.DownloadString("$URL/EnumDevices/$SCHEMA") | 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 ============================================================================"
$geofences = $c.DownloadString("$URL/EnumGeofences/$SCHEMA") | 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 ============================================================="
$oi = $c.DownloadString("$URL/GetOnlineInfoAll/$SCHEMA") | 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