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

Материал из TK
Перейти к: навигация, поиск
(Новая страница: «== Powershell_Basic.ps1 == Нижприведенный файл выполняет несколько запросов и сохраняет их результ…»)
 
 
(не показаны 2 промежуточные версии 1 участника)
Строка 2: Строка 2:
   
 
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV
 
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV
  +
<pre>$URL = "http://m.tk-chel.ru/ServiceJSON";
 
<pre>
 
$URL = "http://m.tk-chel.ru:8300";
 
$SCHEMA = "DemoCEBIT"
 
 
""
 
""
 
Remove-Item "*.csv" -ErrorAction SilentlyContinue
 
Remove-Item "*.csv" -ErrorAction SilentlyContinue
   
 
"== Prepare and Login ========================================================================"
 
"== Prepare and Login ========================================================================"
  +
$Response = Invoke-WebRequest -URI http://m.tk-chel.ru/ServiceJSON
$c = New-Object -TypeName System.Net.WebClient;
 
  +
$loginParms = @{
$c.Headers.Add("Content-Type", "text/json");
 
  +
&nbsp; &nbsp; UserName = "demo"
$token = $c.UploadString("$URL/Login", "{""UserName"":""service-example"",""Password"":""12345678""}").Trim("""");
 
  +
&nbsp; &nbsp; Password="demo"
$c.Headers.Add("AG-TOKEN", $token)
 
  +
}
  +
$token = (Invoke-WebRequest -Uri "$URL/Login" -Method POST -Body $loginParms).Content
 
"Token: $token"
 
"Token: $token"
 
$token > "PS_Token.csv"
 
$token > "PS_Token.csv"
  +
$headers = @{ "AG-Token" = $token }
""
 
   
 
"== EnumSchemas =============================================================================="
 
"== EnumSchemas =============================================================================="
$schemas = $c.DownloadString("$URL/EnumSchemas") | ConvertFrom-Json
+
$schemas = (Invoke-WebRequest -Uri "$URL/EnumSchemas" -Headers $headers).Content | ConvertFrom-Json
 
foreach($x in $schemas) { $x.ID+": "+$x.Name }
 
foreach($x in $schemas) { $x.ID+": "+$x.Name }
 
$schemas | Export-Csv "PS_Schemas.csv" -NoTypeInformation
 
$schemas | Export-Csv "PS_Schemas.csv" -NoTypeInformation
""
 
   
 
"== EnumDevices =============================================================================="
 
"== EnumDevices =============================================================================="
  +
$SCHEMA = $schemas[0].ID
$devices = $c.DownloadString("$URL/EnumDevices/$SCHEMA") | ConvertFrom-Json
 
  +
$devices = (Invoke-WebRequest -Uri "$URL/EnumDevices?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
 
"Groups: "+$devices.Groups.Length
 
"Groups: "+$devices.Groups.Length
 
"Devices: "+$devices.Items.Length
 
"Devices: "+$devices.Items.Length
Строка 33: Строка 32:
   
 
"== EnumGeofences ============================================================================"
 
"== EnumGeofences ============================================================================"
  +
$SCHEMA = $schemas[0].ID
$geofences = $c.DownloadString("$URL/EnumGeofences/$SCHEMA") | ConvertFrom-Json
 
  +
$geofences = (Invoke-WebRequest -Uri "$URL/EnumGeofences?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
 
"Groups: "+$geofences.Groups.Length
 
"Groups: "+$geofences.Groups.Length
 
"Devices: "+$geofences.Items.Length
 
"Devices: "+$geofences.Items.Length
Строка 41: Строка 41:
   
 
"== GetOnlineInfoAll ============================================================="
 
"== GetOnlineInfoAll ============================================================="
  +
$SCHEMA = $schemas[0].ID
$oi = $c.DownloadString("$URL/GetOnlineInfoAll/$SCHEMA") | ConvertFrom-Json
 
  +
$oi = (Invoke-WebRequest -Uri "$URL/GetOnlineInfoAll?schemaID=$SCHEMA" -Headers $headers).Content | ConvertFrom-Json
 
"OnlineInfoItems: "+$oi.Length
 
"OnlineInfoItems: "+$oi.Length
   
Строка 49: Строка 50:
 
foreach($item in $oi)
 
foreach($item in $oi)
 
{
 
{
$obj = New-Object PSObject
+
&nbsp; &nbsp; $obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ID" -Value $item.Key
+
&nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "ID" -Value $item.Key
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $item.Value.Name
+
&nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $item.Value.Name
if($item.Value)
+
&nbsp; &nbsp; if($item.Value)
  +
&nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp;
{
 
$obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value $item.Value.DT
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value $item.Value.DT
$obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value $item.Value.LastPosition.Lat
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value $item.Value.LastPosition.Lat
$obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value $item.Value.LastPosition.Lng
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value $item.Value.LastPosition.Lng
$obj | Add-Member -MemberType NoteProperty -Name "State" -Value $item.Value.State
+
&nbsp; &nbsp; &nbsp; &nbsp; $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
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Latency" -Value $dt.Subtract($item.Value.DT).TotalMinutes
  +
&nbsp; &nbsp; }
}
 
else
+
&nbsp; &nbsp; else
  +
&nbsp; &nbsp; {
{
 
$obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "DTUTC" -Value ""
$obj | Add-Member -MemberType NoteProperty -Name "Speed" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Speed" -Value ""
$obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Lat" -Value ""
$obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Lng" -Value ""
$obj | Add-Member -MemberType NoteProperty -Name "State" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "State" -Value ""
$obj | Add-Member -MemberType NoteProperty -Name "Latency" -Value ""
+
&nbsp; &nbsp; &nbsp; &nbsp; $obj | Add-Member -MemberType NoteProperty -Name "Latency" -Value ""
  +
&nbsp; &nbsp; }
}
 
$oiresult += $obj
+
&nbsp; &nbsp; $oiresult += $obj
 
}
 
}
 
$oiresult | Export-Csv "PS_GetOnlineInfoAll.csv" -NoTypeInformation
 
$oiresult | Export-Csv "PS_GetOnlineInfoAll.csv" -NoTypeInformation

Текущая версия на 10:11, 18 июня 2020

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