locked
Powershell Script Exchange 2013 Help Needed RRS feed

  • Question

  • Hi,

    I'm trying to implement a PowerShell script that will remove any phonesync devices that haven't synced in 110 days. I'm new to PowerShell and saw a few examples on the web but can't get mine to work as needed. I need the script to also output all devices deleted to a time/date stamped CSV.

    Here's my code, any help is appreciated.

    Get-MobileDevice -result unlimited | Get-MobileDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-110”)} | select identity, deviceid, lastsyncattempttime | Export-csv C:\PhoneSync\Logs\Stale_Devices_110days_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv | foreach (Remove-MobileDevice -Identity deviceid -confirm:$false)

    My code is giving me this error:

    The mobile device deviceid cannot be found.
        + CategoryInfo          : NotSpecified: (:) [Remove-MobileDevice], ManagementObjectNotFoundException
        + FullyQualifiedErrorId : [Server=WEBMAIL,RequestId=20842198-67a1-485c-b4d2-c8523be288fc,TimeStamp=1/18/2017 7:15:14 PM] [FailureCategory=Cmdlet
       -ManagementObjectNotFoundException] ABF1147C,Microsoft.Exchange.Management.Tasks.RemoveMobileDevice
        + PSComputerName        : webmail.server.com

    The CSV does export with the correct device(s).

    Wednesday, January 18, 2017 7:31 PM

Answers

  • Got it working with this code:

    $mobileDevices = Get-MobileDevice -result unlimited | Get-MobileDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-110”)} | select identity, deviceid, lastsyncattempttime
    $mobileDevices | Export-csv C:\PhoneSync\Logs\Stale_Devices_110days_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv
    foreach ($device in $mobileDevices){
        $device | Remove-MobileDevice -confirm:$false
    }

    Thanks for not really helping me jrv.


    • Marked as answer by djl236 Thursday, January 19, 2017 3:39 AM
    • Edited by djl236 Thursday, January 19, 2017 3:40 AM
    Wednesday, January 18, 2017 9:15 PM

All replies

  • Hi Djl236

    Try this

    Get-MobileDevice -result unlimited | Get-MobileDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-110")} | foreach (Remove-MobileDevice -Identity $_.deviceid -confirm:$false)

    Good Luck!

    Shane

    Wednesday, January 18, 2017 8:05 PM
  • Start by learning how to format code:

    Get-MobileDevice -result unlimited | 
    	Get-MobileDeviceStatistics | 
    	where { $_.LastSuccessSync -le (Get-Date).AddDays("-110") } |
    	select identity, deviceid, lastsyncattempttime | 
    	Export-csv C:\PhoneSync\Logs\Stale_Devices_110days_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv
    	# You cannot have pieline output past an output CmdLet
    	#| 
    	#foreach (Remove-MobileDevice -Identity deviceid -confirm:$false)
    


    \_(ツ)_/

    Wednesday, January 18, 2017 8:07 PM
  • Got it working with this code:

    $mobileDevices = Get-MobileDevice -result unlimited | Get-MobileDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays(“-110”)} | select identity, deviceid, lastsyncattempttime
    $mobileDevices | Export-csv C:\PhoneSync\Logs\Stale_Devices_110days_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv
    foreach ($device in $mobileDevices){
        $device | Remove-MobileDevice -confirm:$false
    }

    Thanks for not really helping me jrv.


    • Marked as answer by djl236 Thursday, January 19, 2017 3:39 AM
    • Edited by djl236 Thursday, January 19, 2017 3:40 AM
    Wednesday, January 18, 2017 9:15 PM