AutoGRAPH.NET Service Examples Powershell — различия между версиями
Admin (обсуждение | вклад) |
Ivan (обсуждение | вклад) |
||
| Строка 2: | Строка 2: | ||
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV |
Нижприведенный файл выполняет несколько запросов и сохраняет их результаты в файлы .CSV |
||
| − | <pre>$URL = "http://m.tk-chel.ru |
+ | <pre>$URL = "http://m.tk-chel.ru/ServiceJSON"; |
| − | $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.Encoding = [System.Text.Encoding]::UTF8; |
||
| + | UserName = "demo" |
||
| − | $c.Headers.Add("Content-Type", "text/json"); |
||
| + | Password="demo" |
||
| − | $token = $c.UploadString("$URL/Login", "{""UserName"":""demo"",""Password"":""demo""}").Trim(""""); |
||
| + | } |
||
| − | $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 = |
+ | $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 |
||
| Строка 32: | Строка 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 |
||
| Строка 40: | Строка 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 |
||
| Строка 48: | Строка 50: | ||
foreach($item in $oi) |
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 |
$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