我确信我做错了什么,但我正在尝试访问多个 Office 365 powershell 并且它工作正常,但我想在我的网格视图中添加自定义列。一栏写着“客户名称”,另一栏写着其他内容。这就是我到目前为止所得到的。
# Prompt For Login
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'Email Address'
$msg = 'Enter your email address:'
$emailAddress = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
# Connect Office 365
if (Get-Module -ListAvailable -Name ExchangeOnlineManagement) {
Write-Host "Module Exists"
} else {
Write-Host "Module Does not Exist, Installing..."
Install-Module ExchangeonlineManagement
}
$clients = @("ClientA",
"ClientB",
"ClientC",
"ClientD",
"ClientE")
$client = $clients | Out-GridView -Title "Choose a Client" -Passthru
# Make The Connection
Connect-ExchangeOnline -UserPrincipalName $emailAddress -ShowProgress $true -DelegatedOrganization $client
关键是发送对象到
Out-GridView
。
这应该可以帮助你到达你想要的地方:
$clients = [PSCustomObject] @{Client="ClientA";OtherData='Something'},
[PSCustomObject] @{Client="ClientB";OtherData='You'},
[PSCustomObject] @{Client="ClientC";OtherData='Want'},
[PSCustomObject] @{Client="ClientD";OtherData='To'},
[PSCustomObject] @{Client="ClientE";OtherData='Show'}
$choice= $clients | Out-GridView -Title "Choose a Client" -Passthru
#the output from Out-Gridview is now an object, so use dot-notation to get the client.
Connect-ExchangeOnline -UserPrincipalName $emailAddress -ShowProgress $true -DelegatedOrganization $choice.Client
您的问题指出:“...我想添加自定义列...gridview”
答案:
这可以通过在查询中使用 Select-Object cmdlet 来完成。
将其放在 Out-GridView commandlet 之前的管道中,您可以将自定义列添加到输出中。
通过添加标志 -Property 添加您选择的自定义列,并将列添加到输出(在您的情况下,它是 Out-GridView)。
这是一个示例脚本:
<# example for Custom Columns GridView with existing and custom columns #>
Get-Process | `
Sort-Object CPU -Descending | `
Select -First 10 | `
<# Here is where you can select and create columns as you wish: #>
Select-Object -Property `
@{Label = "CPU(s)"; Expression = { if ($_.CPU) { $_.CPU.ToString("N") } } }, `
<# added a Custom Column named "CPU(s)", calculated by using an Expression. #>
@{Label = "MY Custom Column"; Expression = { ("MY " + $_.ProcessName) } }, `
<# added a Custom Column named "MY Custom Column", calculated by using an Expression. #>
ProcessName | `
<# simply selected an existing Column named "ProcessName": #>
Out-GridView -Title "Top 10 CPU processes"
这是 Out-GridView 输出结果的示例:
有关更详细的说明,您可以查看 Microsoft 的这个链接,它解释了如何向标准显示添加属性并实现您所需要的。