2017-08-10 64 views
0

所以,我得到这个脚本:PowerShell是改变脚本的输出

$userName = Read-Host -Prompt 'Input User ID' 
echo "[$userName]" 

$Properties = 
@(
'DisplayName', 
'SamAccountName', 
'Enabled', 
'Created', 
'EmailAddress', 
'title', 
'manager', 
'AccountLockoutTime', 
'Department', 
'Description', 
'Division', 
'LastLogonDate', 
'LockedOut', 
'PasswordLastSet' 
) 

Get-ADUser $userName -Properties $Properties | select $Properties 
Get-ADPrincipalGroupMembership $userName | select name 

当我只执行了最后的命令:

Get-ADPrincipalGroupMembership $userName | select name 

输出是:

name   
----   
Domain Users 
blabla 
blabla Users 
IT.BG   
blabla users 

但是当我运行该脚本输出更改为:

name : Domain Users 

name : blabla 

name : blabla Users 

name : blabla 

name : blabla 

name : blabla users 

有人可以告诉我为什么会发生这种情况,我该如何解决?

+0

不知道为什么这被标记为重复。重复与问题的内容完全相反。答案甚至没有提及'Format-Table',它是回答这个问题所需的cmdlet! –

+0

@BaconBits我将它标记为重复项,因为底层问题是相同的(PowerShell默认输出格式),我相信我对于其他问题的回答解释了为什么会发生这种情况以及如何减轻这种情况。 –

+0

@AnsgarWiechers是的,我认为这是解释PowerShell的默认输出处理的一个很好的参考,但不是一个重复的问题。毕竟,[这个问题](https://stackoverflow.com/questions/2249619/how-to-format-a-datetime-in-powershell)和[这个问题](https://stackoverflow.com/questions/ 9904352/how-to-create-printf-effect-in-powershell)不是duplciates,即使它们都有效地询问“如何使用格式运算符?”。只有在链接问题中的答案完全回答问题时,它才应该是重复的,对吗? –

回答

1

看起来输出系统将其格式化为列表而不是表格。这可能是因为上一行将输出格式化为列表而不是表格。

尝试迫使它是一个表:

Get-ADPrincipalGroupMembership $userName | Format-Table -Property name -Autosize 
+0

它的工作。谢谢! –