我有一個小腳本可以為某些用戶批量設定顯示名稱。但是我的一些用戶在那里的物件中沒有像“名字”和“姓氏”這樣的值。我試圖尋找它,但找不到從我的腳本中跳過這個用戶。
$UPNs = Import-Csv -Path "C:\temp\users.csv" -Delimiter “;”
#.UserPrincipalName, means the value of the first row in CSV file
$Users = ForEach($UPN in $UPNs){Get-MsolUser -UserPrincipalName $UPN.UserPrincipalName | Select-Object Userprincipalname,displayname,firstname,lastname}
$Users | ForEach-Object {
$DisplayName = $_.firstname " " $_.lastname " " "(Admin Account)"
set-msoluser -UserPrincipalName $_.userprincipalname -DisplayName $DisplayName
}
我怎樣才能讓這個腳本說,“沒有‘名字’或‘姓氏’-> 不改變物件”
非常感謝,
里卡多
uj5u.com熱心網友回復:
使用Where-Objectcmdlet:
# Filter out users unless both FirstName and LastName has values
$Users |Where-Object {$_.FirstName -and $_.LastName} | ForEach-Object {
$DisplayName = $_.firstname " " $_.lastname " " "(Admin Account)"
set-msoluser -UserPrincipalName $_.userprincipalname -DisplayName $DisplayName
}
在這里,我們使用-and布爾邏輯運算,以確保雙方$_.FirstName和$_.LastName具有的值是truthy -$null和空字串將計算為$false在布爾背景關系,與其他任何字串值,將評估到$true。
如果您還想跳過其中任何一個只包含空格,請使用靜態[string]::IsNullOrWhiteSpace()方法
# Filter out users unless both FirstName and LastName has non-whitespace-only values
$Users |Where-Object {-not([string]::IsNullOrWhiteSpace($_.FirstName)) -and -not([string]::IsNullOrWhiteSpace($_.LastName))} | ForEach-Object {
$DisplayName = $_.firstname " " $_.lastname " " "(Admin Account)"
set-msoluser -UserPrincipalName $_.userprincipalname -DisplayName $DisplayName
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331881.html
標籤:电源外壳
