首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

利用Powershell分析近来一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息

2013-10-17 
利用Powershell分析最近一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息!近期有朋友问

利用Powershell分析最近一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息!

近期有朋友问我想从服务器中提取出最近几天的用户使用activesync 信息怎么办?使用powershell 的时候我们必须要知道的是怎么做才能得到自己想要的信息,我们现在使用的方法第一步是查询有使用过手机的用户,这个我们采用如下的命令完成,我们先看下我们都需要一些什么命令:


get-casmailbox 获取当前邮箱各个功能使用状态的powershell命令

get-aduser  获取当前用户的各个AD 属性

get-activesyncdevice 获取当前用户拥有的使用activesync 协议的移动设备信息

get-activesyncdevicestatistics 获取当前设备的连接信息


整体来说语句如下:

param

#定义脚本全局参数
(
[string] $devicelist="c:\device\",

#定义日志路径
[datetime] $recenttime=(Get-Date).adddays(-2000)

#定义最后同步日期
)
Import-Module ac*

#由于使用了ad命令,因此必须导入ad模块
$getuserinfo=(get-casmailbox -filter {HasActiveSyncDevicePartnership -eq $true -and -not displayname -like "cas_{*"} |get-mailbox).samaccountname

#得出拥有移动设备的用户,并得出用户的samaccountname 列表

$userinfos=@()

#预定义用户信息列表
foreach($currentuser in $getuserinfo)

#依据得出用户信息进行轮询
   {
      $usercompany=(get-aduser -identity $currentuser -property *).company

     #得出用户所在的公司
     $userdepartment=(get-aduser -identity $currentuser -property *).department

     #得出用户所在的部门
     $userdisplayname=(get-aduser -identity $currentuser -property *).displayname

     #得出用户的显示名称
     $DeviceGuids=(get-activesyncdevice -mailbox $currentuser).guid

    #求出用户拥有的设备列表,因为一个用户可能存在多个设备,由于当前的数值是guid ,不能作为其他的命令参数,需要将guid转换成string
     $devguids=@()

    #定义字符串数组
       foreach ($deviceguid in $DeviceGuids)

   #根据GUID进行轮询
      {
         $devguid=$deviceguid.tostring()

   #将GUID转换为字符串
         $devguids=$devguids+$devguid

  #将GUID 字符串添加到数组里
          }
 
 
           foreach($devguid in $Devguids)

  #根据用户guid进行循环,一个用户有多个设备,因此有多个GUID
             {
                $Devicetype=(get-activesyncdevicestatistics -identity $devguid).devicetype

                 #获取当前设备的类型
                 $DeviceSyncSuccess=(get-activesyncdevicestatistics -identity $devguid).Lastsuccesssync

                #获取设备上一次同步时间
                 $userinfo=New-Object psobject

                #新建一个Powershell 对象
                      if ($DeviceSyncSuccess -gt $recenttime)

                     #定义在我们初期定义的时间之后的时间
                           {
                               $userinfo |Add-Member -MemberType NoteProperty -Name "DeviceGuid" -Value $devguid

                              #增加一列Geviceguid属性
                               $userinfo |Add-Member -MemberType NoteProperty -Name "UserDisplayname" -Value $userdisplayname

                              #增加一列用户的显示名称属性
                               $userinfo |Add-Member -MemberType NoteProperty -Name "UserCorp" -Value $usercompany

                              #增加一列用户公司的属性
                               $userinfo |Add-Member -MemberType NoteProperty -Name "UserDepartment" -Value $userdepartment

                             #增加一列公司属性
                               $userinfo |Add-Member -MemberType NoteProperty -Name "UserDeviceType" -Value $Devicetype

                            #增加一列设备类型属性
                              $userinfo |Add-Member -MemberType NoteProperty -Name "UserLastSuccesstime" -Value $DeviceSyncSuccess

                              #增加一列用户上次同步时间属性
                               $userinfos=$userinfos+$userinfo

                             #将用户相应属性增加到相应的对象功能
  }
 
  }


}
$exportcsvdevice=$devicelist+"Device"+(Get-Date).tostring("yyyy-MM-dd")+".csv"

#定义导出设备列表名称
$userinfos |Export-Csv -Path $exportcsvdevice -Encoding unicode

#将所取得的用户属性列表导出成csv 文件


针对这个脚本执行我们可以采用两种方式,方式一,直接执行,不加任何参数:

利用Powershell分析近来一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息

也可以在执行参数中定义上次同步时间周期,执行方式如下:

利用Powershell分析近来一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息


结果生成了csv 文件,我们将文件打开后,内容如下:


利用Powershell分析近来一段时间使用手机的activesync 协议进行连接的用户和相关的账户信息

希望对大家有所帮助!

??

热点排行