我正在尝试输出从用于连接到MDM控制台的API提取的项目列表。我有两个函数,这两个函数可以一起工作,并且可以在脚本的输出中看到所有数据,但是,我想使用其中一个函数的变量名将此列排序为.csv文件。标头。我是PS的新手,一直在拔头发。 我可以看到变量是一个数组,数组中的所有值都在其中,但是对于如何排序是否有更好的方法来获取所需的数据,我一无所知。
#Current MDM Environment
$ev = "Q"
#Define MDM credentials to match environment from above
if($ev -eq "Q")
{
$Code = 'VbvmMGOV0Pd2lF4GurpBqnwD/R6mFmUKI6z3CKAY5tw='
$ui = 'MDMqualserver'
}
else{$Code = 'Pe8w/3jDREgse2gUu3UYZ28FHeafg0xcheu/AYwJ6PE='
$ui = 'MDMprodserver'}#>
#API Auth for MDM Console
$Auth = Get-Content -path 'C:\ProgramData\ScriptAuth\mobilityapi.txt'
$Contenttype = 'application/json'
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy')
$path = "C:\users\username\desktop\$currentDate.csv"
Function get_all
{
$array =@()
#Define URL
$url = "https://$ui.company.gov/api/mdm/devices/extensivesearch?
pagesize=10000"
#Define Headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],
[String]]"
$headers.Add("aw-tenant-code", $Code)
$headers.Add("Authorization", $Auth)
#Send Rest Request
try{
$response = Invoke-RestMethod -uri $url -Headers $headers}
catch{
$error = "BDevice Info Not Found"}
#Close Connection
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($url)
$SSP = $ServicePoint.CloseConnectionGroup("")
#Parse Device Info
$data = $response.DeviceExtensiveSearchResult.Devices.DeviceDetailsExt
$data | foreach {
$serial = $_.SerialNumber
$array += $serial
}
return $array
}
Function get_devattrib
{
Param([string]$serial)
$array = @()
#Define URL
$url = "https://$ui.company.gov/api/mdm/devices?
searchby=Serialnumber&id=$serial"
#Define Headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],
[String]]"
$headers.Add("aw-tenant-code", $Code)
$headers.Add("Authorization", $Auth)
$headers.Add("Content-Type", $Contenttype)
#Send Rest Request
try{
$response = Invoke-RestMethod -uri $url -Headers $headers}
catch{
return $false}
#Close Connection
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($url)
$ServicePoint.CloseConnectionGroup("")
#Parse Device Info (#removed .Device from $data = $response.Device)
$data = $response
$data | foreach {
$ownership = $_.Ownership
$friendlyname = $_.DeviceFriendlyName
$platform = $_.Platform
$model = $_.Model
$snumber = $_.AssetNumber
$username = $_.UserName
$mac = $_.MacAddress
$phone = $_.PhoneNumber
$lastseen = $_.LastSeen
$enrollstatus = $_.EnrollmentStatus
$compliance = $_.ComplianceStatus
return [datetime]$lastseen, $ownership, $friendlyname, $platform, $model,
$snumber, $serial, $username, $mac, $phone, $enrollstatus, $compliance
}
}
$devices = @()
$getdevices = @()
$devices = get_all
foreach ($device in $devices){
$getdevices += get_devattrib $device
}
$getdevices
控制台中每台设备的数据输出如下所示(我已将信息设置为通用信息以隐藏公司数据):
是
2018年9月5日,星期三,下午8:33:21企业拥有的用户名-资产编号具有Wi-Fi +蜂窝网络(128 GB太空灰)资产的Apple iPad Pro资产编号串行数字用户名mac地址电话号码已注册
A。我不明白为什么在控制台中每个设备的输出开头都会得到“ true”,然后在空格之后显示([datetime]
似乎是这样)
B。我不明白如何将所有这些数据放在.csv文件中。我确实在函数之前定义了一条路径,并且知道如何使用export-csv,但是它发送到文件的数据仅显示为#TYPE System.Boolean
,所以我假设我的最后一个变量有问题。哇。
答案 0 :(得分:4)
您可以使用Sort-Object
进行排序。
$ArrayOfStrings = 's', 't', 'a', 'c', 'k'
$ArrayOfStrings | Sort-Object