如果不存在,请创建用户Office 365
嗨;
我有一个超过100个用户的列表(csv文件)。 我的csv文件包含以下列:
$
UserPrincipalName,LastName,FirstName,DisplayName
csv文件中的某些用户拥有Office 365中的帐户,有些则没有。 我想使用Powershell浏览列表。 如果用户拥有Office 365帐户,则Powershell将显示一条消息,指出用户已在Office 365中存在
。如果用户没有Office 365帐户,则Powershell将为其创建帐户那个用户。 以下是我的想法(我已经在Powershell中连接到Office 365和MSOL服务):
$
$ Accounts = Import-csv .. \ * .csv
ForEach($ account中的帐户)
{
$ AccountExists = Get-MsolUser -UserPrincipalName $ account .UserPrincipalName
If($ AccountExists -ne $ Null){写入主机'$($ Account.Displayname)存在于Office 365中}}
Else {New- MsolUser -UserPrincipalName $ Account.UserPrincipalName,-LastName $ Account.LastName,-FirstName $ Account.FirstName,-DisplayName $ Account.Displayname}
}
当我运行此操作时,我收到以下错误,它与创建新用户有关:
$
Get-MsolUser:User Not发现。 用户:XXXX @ XXX.com
$
在C:\ PS_Scripts\test11512.ps1:4 char: 30
+ $ AccountExists = Get-MsolUser<<<< -UserPrincipalName $ account.UserPrincipal
名称
&NBSP; + CategoryInfo &NBSP; &NBSP; &NBSP; :OperationStopped:(:) [Get-MsolUser],MicrosoftO
nlineException
&NBSP; + FullyQualifiedErrorId:Microsoft.Online.Administration.Automation.UserN
otFoundException,Microsoft.Online.Administration.Automation.GetUser
I have a list (csv file) of over 100 users. My csv file has the following columns:
UserPrincipalName, LastName, FirstName, DisplayName
Some of the users in the csv file have accounts in Office 365 and some do not. I would like to use Powershell to go through the list. If a user has an Office 365 account, then Powershell will display a message stating that the user already exists
in Office 365. If the user does not have an Office 365 Account, Powershell will create an account for that user. Here is what I’m thinking (I’ve already connected to Office 365 and MSOL services in Powershell):
$Accounts = Import-csv ..\*.csv
ForEach ($account in $accounts)
{
$AccountExists = Get-MsolUser -UserPrincipalName $account.UserPrincipalName
If ($AccountExists -ne $Null) {Write-host "$($Account.Displayname) exists in Office 365"}
Else {New-MsolUser -UserPrincipalName $Account.UserPrincipalName, -LastName $Account.LastName, -FirstName $Account.FirstName, -DisplayName $Account.Displayname}
}
When I run this, I get the following errors and it has to do with creating the new user:
Get-MsolUser : User Not Found. User: XXXX@XXX.com
At C:\PS_Scripts\test11512.ps1:4 char:30
+ $AccountExists = Get-MsolUser <<<< -UserPrincipalName $account.UserPrincipal
Name
+ CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftO
nlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserN
otFoundException,Microsoft.Online.Administration.Automation.GetUser
如何从输出中显示此错误消息?
How do not show this error message from the output ?
问候
Hello Djamel,
Hello Djamel,
基本上有两种方法可以解决这个问题:快速而肮脏的方式和更明智,但更耗时的方式。
there are basically two ways to go about this: The quick and dirty way and the more wise, but more time-intensive way.
# Quick and Dirty:
# Append this to command
-ErrorAction 'SilentlyContinue'
# A bit more effort:
try
{
[Your command]
}
catch
{
[If this one error, ignore it, otherwise report it]
}
快速和肮脏需要半分钟,但意味着您将不会看到由Get-MsolUser生成的 y错误。有关更智能但更耗时的解决方案如何运作的更多信息,请使用:
Quick and dirty takes half a minute, but means you won't be shown any errors generated by Get-MsolUser. For more info on just how the smarter but more time-intensive solution works, use:
Get-Help about_try
干杯,$
Fred
Cheers,
Fred