-1

我有一个脚本,它使用 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 号码,并且没有小数,也不会被截断。

4

1 回答 1

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

请参阅:有关技术的说明的Select-Object文档。-Property

于 2019-03-29T20:30:59.723 回答