O365 移动设备导出

O365 Mobile Device Export

以下代码在我们的内部交换服务器上运行,减去了连接位,因为本地不需要。我已将连接代码添加到 365,效果很好。当我 运行 这样做时,出现以下错误,它迫使我再次登录并仅导出结果的一小部分:

    Starting a command on the remote server failed with the following error message : The I/O operation has been aborted
because of either a thread exit or an application request. For more information, see the about_Remote_Troubleshooting
Help topic.
    + CategoryInfo          : OperationStopped: (ps.outlook.com:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : ps.outlook.com

Processing data from remote server ps.outlook.com failed with the following error message: WS-Management cannot
process the request. The operation failed because of an HTTP error. The HTTP error (12152) is: The server returned an
invalid or unrecognized response . For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OperationStopped: (ps.outlook.com:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : ps.outlook.com

下面是我尝试使用的代码。我的格式有误吗?

#Script Created by Daniel Taylor 8/8/18

#Set Location for export:
$fLocation = "C:\temp\"

#Get username and password for 0365 connection
$cred = get-credential

#Import microsoft online
Import-module msonline

#Connect to MSonline
connect-msolservice -Credential $cred

#Connect to exchange online
$EolSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/” -Credential $cred -Authentication Basic -AllowRedirection

Import-PSSession $EolSession -DisableNameChecking

Write-host "You are now connected to Exchange. Begning ActiveSync Export:" -ForegroundColor DarkGreen

#create File to write report to:
$fName = $fLocation+"ActiveSyncDevices.txt"
$test = test-path $fName
    if ($test -eq $True)
        {
            write-host "Removing Old File..." -ForeGroundColor Red
            Remove-Item $fName
        }
    #Else
        #{
            #New-Item $fName -type file
        #}
Write-host "Creating New File..." -ForeGroundColor Green
New-Item $fName -type file

#Get ActiveSync and Mailbox data
$EASDevices = ""
$AllEASDevices = @()

$EASDevices = ""| select 'User','PrimarySMTPAddress','DeviceType','DeviceModel','DeviceOS', 'LastSyncAttemptTime','LastSuccessSync'
$EasMailboxes = Get-Mailbox -ResultSize unlimited
foreach ($EASUser in $EasMailboxes) {
$EASDevices.user = $EASUser.displayname
$EASDevices.PrimarySMTPAddress = $EASUser.PrimarySMTPAddress.tostring()
    foreach ($EASUserDevices in Get-MobileDevice -Mailbox $EasUser.alias) {
$EASDeviceStatistics = $EASUserDevices | Get-MobileDeviceStatistics
    $EASDevices.devicetype = $EASUserDevices.devicetype
    $EASDevices.devicemodel = $EASUserDevices.devicemodel
    $EASDevices.deviceos = $EASUserDevices.deviceos
$EASDevices.lastsyncattempttime = $EASDeviceStatistics.lastsyncattempttime
$EASDevices.lastsuccesssync = $EASDeviceStatistics.lastsuccesssync
    $AllEASDevices += $EASDevices | select user,primarysmtpaddress,devicetype,devicemodel,deviceos,lastsyncattempttime,lastsuccesssync
    }
    }
$AllEASDevices = $AllEASDevices | sort user
$AllEASDevices | Export-Csv $fname

write-host "The script completed successfully! The output file can be found at $fName" -ForeGroundColor Yellow

您不能同时登录 Exchange on-prem 和 Exchange online。他们使用相同的 cmdlet,本地优先。

如果您使用 PSRemoting,则必须使用两个不同的会话,最好在它们前面加上前缀,这样它们是唯一的。

或者,直接在 Exchange 服务器上并在那里做你的事,并打开一个新的 PSRemoting 会话到 O365,添加前缀以便代理命令具有前缀名称。

然后使用代理的 cmdlet 进行 O365 调用,而不是本地的。

示例会话设置:

# Exchange PowerShell Remote Management
#
$creds = Import-Clixml -Path $CredPath 

$ExchangeSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' `
-ConnectionUri ('http://' + $ExchangeServerFQDN + '/PowerShell') `
-Authentication Kerberos -Credential $Creds.ExpAdmin
Import-PSSession $ExchangeSession -Prefix 'EXP'


$creds = Import-Clixml -Path $CredPath 

$ExchangeOnlineSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' `
-ConnectionUri 'https://outlook.office365.com/PowerShell-liveid/' `
-Authentication Basic -Credential $Creds.ExoAdmin -AllowRedirection
Import-PSSession $ExchangeOnlineSession -Prefix 'EXO'

Get-PSSession

每个命令都将 EXP 或 EXO 作为代理 cmdlet 名称的一部分。 同样,如果您直接在 Exchange 上执行此操作,则只需要 EXO 会话,并正常使用本地 Exchange cmdlet。