从不同变量组合创建表

时间:2014-12-03 07:22:55

标签: powershell-v2.0

下面是我编写的脚本,用于从Active Directory获取一些用户信息以及组成员身份以及帐户的邮箱可用性。

Import-Module Activedirectory
$ou = Read-Host "Please enter the OU Distinguished Name eg: OU=APAC,DC=kabwahib,DC=local"
$users = Get-ADUser -Filter * -Properties * -SearchBase $ou

foreach ($user in $users)
{
    $internet = Get-ADPrincipalGroupMembership $user | where {$_.name -eq "G_internetaccess_full"} | select Name
    $vpn = Get-ADPrincipalGroupMembership $user | where {$_.name -eq "G_RemoteAccessAllowed"} | select Name

if ($user.msExchRecipientTypeDetails -eq $null)

{
$mailbox = "False"
}
else
{
$mailbox = "True"
}

if ($internet -eq $null)
{
$internet = "False"
}
else
{
$internet = "True"
}

if ($vpn -eq $null)
{
$vpn = "False"

}
else 
{
$vpn = "True"
}

$report = New-Object PSObject
$report | Add-Member Noteproperty "User ID" $user.samaccountname
$report | Add-Member Noteproperty "DisplayName" $user.DisplayName
$report | Add-Member Noteproperty "Job Title" $user.Title
$report | Add-Member Noteproperty "Email" $mailbox
$report | Add-Member Noteproperty "VPN" $vpn
$report | Add-Member Noteproperty "Internet" $internet

$report | Format-Table | Out-File "C:\Scripts\ADquery\userlist.csv" -Append

}

现在的问题是,我得到的输出不是我想要的方式。

当前输出样本:

User ID       DisplayName   Job Title    Mailbox      VPN          Internet    
-------       -----------   ---------    -------      ---          --------    
Test2         Test2                      True         True         False       



User ID       DisplayName   Job Title    Mailbox      VPN          Internet    
-------       -----------   ---------    -------      ---          --------    
test10        Test10                     True         False        True        



User ID       DisplayName   Job Title    Mailbox      VPN          Internet    
-------       -----------   ---------    -------      ---          --------    
test11        Test11                     True         False        True        



User ID       DisplayName   Job Title    Mailbox      VPN          Internet    
-------       -----------   ---------    -------      ---          --------    
test12        Test12                     True         False        True    

我的预期结果:

User ID       DisplayName   Job Title    Mailbox      VPN          Internet    
-------       -----------   ---------    -------      ---          --------    
Test2         Test2                      True         True         False       
test10        Test10                     True         False        True        
test11        Test11                     True         False        True         
test12        Test12                     True         False        True         
test13        Test13                     True         False        True          
test14        Test14                     True         False        True        

任何人都可以帮我查一下如何消除重复标题并在一个漂亮的csv文件中生成我的预期结果吗?

2 个答案:

答案 0 :(得分:0)

从导出中删除Format-Table

$report | Out-File "C:\Scripts\ADquery\userlist.csv" -Append

发生的事情是,每次拨打Format-Table时,都会生成一个新表格。此外,生成的表格已格式化为控制台输出,并且不会正确转换为.csv文件。

答案 1 :(得分:0)

感谢您的回复。我已经想出如何实现它。以下是我更新的脚本。

Import-Module Activedirectory $ ou =读取主机“请输入OU专有名称,例如:OU = APAC,DC = kabwahib,DC = local” $ users = Get-ADUser -Filter * -Properties * -SearchBase $ ou

$colreports = @()
foreach ($user in $users)
{
    $internet = Get-ADPrincipalGroupMembership $user | where {$_.name -eq "G_internetaccess_full"} | select Name
    $vpn = Get-ADPrincipalGroupMembership $user | where {$_.name -eq "G_RemoteAccessAllowed"} | select Name

if ($user.msExchRecipientTypeDetails -eq $null)

{
$mailbox = "False"
}
else
{
$mailbox = "True"
}

if ($internet -eq $null)
{
$internet = "False"
}
else
{
$internet = "True"
}

if ($vpn -eq $null)
{
$vpn = "False"

}
else 
{
$vpn = "True"
}

$report = New-Object PSObject
$report | Add-Member Noteproperty "User ID" $user.samaccountname
$report | Add-Member Noteproperty "DisplayName" $user.DisplayName
$report | Add-Member Noteproperty "Job Title" $user.Title
$report | Add-Member Noteproperty "Email" $mailbox
$report | Add-Member Noteproperty "VPN" $vpn
$report | Add-Member Noteproperty "Internet" $internet


$colreports += $report

#$report | Format-Table -HideTableHeaders| Out-File "C:\Scripts\ADquery\userlist.csv" -Append

}

$colreports | Export-Csv "C:\Scripts\ADquery\userlist.csv" -NoTypeInformation -Delimiter "|"

我已添加以下行,将所有结果收集到一个变量中,并将其全部写入csv中。

 $colreports += $report