O365移动设备导出

时间:2018-08-08 20:43:04

标签: powershell exchange-server

以下代码在我们的内部交换服务器上有效,减去了连接位,因为这不需要本地部署。我已将连接代码添加到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

1 个答案:

答案 0 :(得分:0)

您无法同时登录到本地Exchange和在线Exchange。他们使用相同的cmdlet,本地优先。

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

或者,直接在Exchange服务器上执行操作,然后打开一个新的以O365为前缀的PSRemoting会话,以使代理命令具有前缀名称。

然后使用代理的cmdlet(而不是本地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

每个命令的代理cmdlet名称中都会包含EXP或EXO。 同样,如果直接在Exchange上执行此操作,则只需要EXO会话,并通常使用本地Exchange cmdlet。