Office365中获取部门名称

Office365中获取部门名称

本文介绍了Foreach循环在Powershell Office365中获取部门名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从UserPrincipalNames列表中获取部门

I am trying to get the department from a list of UserPrincipalNames

我可以让它在foreach循环之外为单个用户使用.它增加了我遇到麻烦的循环.

I am able to get this to work for a single user outside of the foreach loop. Its adding the loop where I am having trouble.

Connect-MsolService
$users = Import-Csv C:\Users\me\Desktop\users.csv

foreach ($user in $users){

Get-MsolUser -UserPrincipalName $user | Select-Object firstname, lastname, UserPrincipalName, department |Export-Csv C:\Users\me\Desktop\test.csv

}

CSV中列出了50个电子邮件地址,每行一个电子邮件地址.第一行是"UserPrincipalName"

There are 50 email addresses listed in the CSV one email address per line. With the first line being "UserPrincipalName"

CSV样本数据

userprincipalname
[email protected]
[email protected]
[email protected]
[email protected]

推荐答案

思考PowerShell-管道使用对象,并且您有ForEach-Object cmdlet:

Think PowerShell - the pipeline uses objects, and you have a ForEach-Object cmdlet:

Connect-MSOLService

Import-CSV -Path C:\Users\Me\Desktop\Users.CSV |
    ForEach-Object { Get-MSOLUser -UserPrincipalName $_.UserPrincipalName |
        Select-Object firstname, lastname, UserPrincipalName, department } |
    Export-CSV C:\Users\Me\Desktop\test.CSV

这篇关于Foreach循环在Powershell Office365中获取部门名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:19