我為日歷授權運行一個所有用戶命令。然后我報告。問題是我如何包括沒有日歷授權的人?(下面的代碼)
在這一行 If ($null -ne $DelegateCal) 我確定某人有日歷授權,然后建立物件。
如果我不使用 =,當我為$null添加一個Else時,我不確定如何建立物件。
<#All Google Calendar delegate report#>/span>
# filename函式
進口-模塊C:asksModulesMRNAPMRNAP.psm1
$AllGoogleUsers = gam print users fields suspended | ConvertFrom-CSV | Where-Object { $_. suspended -eq $False } | Select-Object -ExpandProperty PrimaryEmail
ForEach ($UserEmail in $AllGoogleUsers) {
$DelegateCal = gam calendar $UserEmail print acls | convertfrom-csv | Where-Object { $_. 'scope.type' -eq 'user' -and $_. 'Scope.value' -ne $UserEmail }。-ErrorAction SilentlyContinue
If ($null -ne $DelegateCal) {
$CalendarDelegateList = foreach ($line in $DelegateCal) {
[PSCustomObject]@{
所有者 = $line.calendarId
型別 = 'Calendar'$line.'scope.value'
角色 = $line.角色
}
}
}
}
$CalendarDelegateList = $CalendarDelegateList | Sort-Object -Property Owner
$filename = MRNAP -ReportName WhoIsCalendarDelegated -Move
$CalendarDelegateLis | Export-Csv $filename -NoTypeInformation | Format-table text-align=left -AutoSize
這是我用 =
$AllGoogleUsers = gam print users fields suspended | ConvertFrom-Csv | Where-Object { $_. suspended -eq $False } | Select-Object -ExpandProperty PrimaryEmail
ForEach ($UserEmail in $AllGoogleUsers) {
$DelegateCal = gam calendar $UserEmail print acls | convertfrom-csv | Where-Object { $_. 'scope.type' -eq 'user' -and $_. 'Scope.value' -ne $UserEmail }。-ErrorAction SilentlyContinue
If ($null -ne $DelegateCal) {
foreach ($line in $DelegateCal) {
$CalendarDelegateList = [PSCustomObject]@{
所有者=$UserEmail
型別 = 'Calendar'/span>
授權 = $line.'scope.value'.
角色 = $line.角色
}
}
}
Else {
$CalendarDelegateList = [PSCustomObject]@{
業主 = $UserEmail $UserEmail
型別 = 'Calendar'/span>
授權 = 'None'
角色 = 'None'/span>
}
}
}
uj5u.com熱心網友回復:
讓PowerShell在陣列中為你收集陳述句輸出,而不是手動建立輸出串列--無論是為了簡潔還是為了性能;更多資訊請參見本答案。
這甚至適用于嵌套的
。這甚至適用于嵌套的 foreach 回圈,正如您的情況一樣。
應用于你的方案(節選):
[array] $CalendarDelegateList =
foreach ($UserEmail in $AllGoogleUsers) {
$DelegateCal = gam calendar $UserEmail print acls | ConvertFrom-CSV | Wher-Object { $_. 'scope.type' -eq 'user' -and $_.'Scope.value' -ne$UserEmail } -ErrorAction SilentlyContinue
如果($null -ne $DelegateCal) {
foreach ($line in $DelegateCal) {
# Construct * and output* a [pscustomobject] instance.
[PSCustomObject]@{
業主 = $UserEmail
# ...
}
}
}
否則 {
[PSCustomObject]@{
業主=$UserEmail。
# ...。
}
}
}
所有從[pscustomobject]回圈內部輸出的foreach實體(無論是直接輸出還是從嵌套輸出)被自動收集到變數$CalendarDelegateList中。
注意:
如果回圈中有兩個或更多的輸出物件,
$CalendarDelegateList變數會收到一個常規的PowerShell陣列(型別為[object[]])。[array]型別約束(簡稱:[object[]])另外確保結果是一個陣列,即使回圈只輸出一個物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/309297.html
標籤:
