我有一个脚本,该脚本使用API从我们的MDM中提取有关电话设备的数据。它会创建一个包含许多列的.csv文件。 IMEI列显示为(例如)3.343434 + 14,而不是334343412345678912345。有人可以帮助我显示如何在.csv文件中正确显示此输出吗?事实上,我可以操纵列的属性,但宁愿它只是正确的。当我真的希望它是一个数字/整数时,输出看起来像一般的一样。
我不知道在哪里可以输入[int](如果要解决这个问题,甚至需要输入)。
$data = $response.Device
$data | foreach {
$serial = $_.SerialNumber
$phone = $_.PhoneNumber
$ownership = $_.Ownership
$enrollstat = $_.EnrollmentStatus
$compliant = $_.ComplianceStatus
$user = $_.UserName
$asset = $_.AssetNumber
$getlast = $_.LastSeen
$imei = $_.Imei
$lastdate = [DateTime]$getlast
try{$lastdate = Get-Date $getlast}
catch{write-host "NULL Date $serial"}
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'Serial Number' -MemberType NoteProperty -Value $serial
$object | Add-Member -Name 'Phone Number' -MemberType Noteproperty -Value $phone
$object | Add-Member -Name 'IMEI' -MemberType NoteProperty -Value $imei
$object | Add-Member -Name 'Ownership' -MemberType Noteproperty -Value $ownership
$object | Add-Member -Name 'Enrollment Status' -MemberType Noteproperty -Value $enrollstat
$object | Add-Member -Name 'Compliance Status' -MemberType Noteproperty -Value $compliant
$object | Add-Member -Name 'User' -MemberType Noteproperty -Value $user
$object | Add-Member -Name 'Asset Number' -MemberType Noteproperty -Value $asset
$object | Add-Member -Name 'Last Seen Date' -MemberType NoteProperty -Value $lastdate
我希望.cvs列显示完整的IMEI号,并且没有小数,也不能被截断。
答案 0 :(得分:0)
我无法复制您的问题,但我想展示实现您的目标的正确方法:
# assumption: $response is the result of some REST API call using `Invoke-RestMethod`
# `Invoke-RestMethod` will automatically turn xml/json responses into objects
$response = [pscustomobject]@{
Device = [pscustomobject]@{
SerialNumber = 'abc123'
PhoneNumber = '+18005551234'
Ownership = 'UNIQUEID'
EnrollmentStatus = $true
ComplianceStatus = $false
UserName = 'USERID'
AssetNumber = 123456
LastSeen = '2019-03-29T12:00:00'
Imei = 334343412345678912345
}
}
$response.Device | Select-Object -Property @(
@{
Name = 'Serial Number'
Expression = {$_.SerialNumber}
}
@{
Name = 'Phone Number'
Expression = {$_.PhoneNumber}
}
@{
Name = 'IMEI'
Expression = {$_.Imei}
}
@{
Name = 'Ownership'
Expression = {$_.Ownership}
}
@{
Name = 'Enrollment Status'
Expression = {$_.EnrollmentStatus}
}
@{
Name = 'Compliance Status'
Expression = {$_.ComplianceStatus}
}
@{
Name = 'User'
Expression = {$_.UserName}
}
@{
Name = 'Asset Number'
Expression = {$_.AssetNumber}
}
@{
Name = 'Last Seen Date'
Expression = {$_.LastSeen}
}
) | Export-Csv -Path C:\Temp\test.csv -NoTypeInformation
-Property
上的Select-Object
documentation,以获取技术说明。