-1

I have a script that uses APIs to pull data from our MDM about phone devices. It creates a .csv file with many columns. The IMEI column shows as (for instance) 3.343434+14 instead of showing 334343412345678912345. Can anyone help show me how to have this output properly in the .csv file? I can manipulate the column properties after the fact, but would rather it just came out correct. It appears the output is coming through as general when I really want it to be a number/integer.

I can't figure out where to possible enter [int] (if that's even what is required to fix this).

$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

I would like the .cvs column to show the entire IMEI number and not have decimals nor be truncated.

bdjenky
  • 23
  • 1
  • 7
  • Why are you doing all that extra work? `$response.Device | Export-Csv ...` – Maximilian Burszley Mar 29 '19 at 20:16
  • If you need the columns to have certain names, utilize calculated properties found in the documentation for `Select-Object`: `... | Select-Object @{N='User'; L={$_.UserName}} | Export...` – Maximilian Burszley Mar 29 '19 at 20:17
  • Lastly, your code example is missing the key part of your problem: the csv conversion. – Maximilian Burszley Mar 29 '19 at 20:18
  • Thanks for the information, I did mistakenly leave out the conversion which is using Export-Csv -Path $path -NoTypeInformation. I'm very new to any coding, and am attempting learn OTJ. Is what you're showing above an example of a hash table? I am also not exporting the .csv until the end after some foreach statements do some other work with other arrays, not sure if that matters. – bdjenky Mar 29 '19 at 20:46
  • See my answer; it's an example of using a hashtable as a property to calculate properties on the resulting object. – Maximilian Burszley Mar 29 '19 at 20:46
  • Are you looking at the CSV file in Excel or Notepad? I have had it where the underlying CSV is correct, but Excel was displaying the Shortened value. – HAL9256 Mar 29 '19 at 20:52
  • It looks great, much less typing as well, I will give that shot, thank you! Looking at .csv in Excel. – bdjenky Mar 29 '19 at 20:53
  • Notepad definitely shows the IMEI correctly, with the full string of digits. – bdjenky Mar 29 '19 at 20:55

1 Answers1

0

I was unable to replicate your problem, but I wanted to showcase the proper way to go about your goal:

# 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

See: Select-Object documentation on -Property for an explanation of technique.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Why not directly use the proper name in the `[PScustomObject]@{'Serial Number'='abc123';...´? –  Mar 29 '19 at 20:40
  • 1
    @LotPings I was mimicing their response object output which is coming from an API. This is a self-contained answer without access *to* their API – Maximilian Burszley Mar 29 '19 at 20:44