我有一个 powershell 脚本,我想将 get-localgroupmember 的信息(针对管理员)输出到控制台,然后(单独)将 get-localgroupmember 的信息(针对用户)输出到控制台。但是当我运行这两个脚本时,结果会合并,第一次调用时不会显示输出,然后响应第二次/最后一次调用,所有结果都位于同一个对象类列表中。
我如何让它一次单独执行一个?
$userName = "Student"
Write-Host "`nShowing local user`n"
Get-LocalUser -Name $userName;
Write-Host "`nShowing Users group`n";
Get-LocalGroupMember -Group "Users";
Write-Host "`nShowing Administrators group`n";
Get-LocalGroupMember -Group "Administrators";
Write-Host "`nShowing local users`n"
Get-LocalUser -Name $userName;
我尝试将每个变量分配给它们自己的变量,然后使用 write-host 输出每个变量,但它仍然做同样的事情。
这是我尝试过的,得到了相同的输出
Write-Host "`nShowing Users group`n";
$object = Get-LocalGroupMember -Group "Users";
Write-Output $object
Write-Host "`nShowing Administrators group`n";
$object = Get-LocalGroupMember -Group "Administrators";
Write-Output $object
如果我将它们一起运行,我会得到以下结果:
Showing local user
Showing Users group
Showing Administrators group
Name Enabled Description
---- ------- -----------
Student True
Name : MSI\user
SID : S-1-5-21-1457208496-2238809947-3401341024-1001
PrincipalSource : MicrosoftAccount
ObjectClass : User
Name : MSI\student
SID : S-1-5-21-1457208496-2238809947-3401341024-1004
PrincipalSource : Local
ObjectClass : User
Name : NT AUTHORITY\Authenticated Users
SID : S-1-5-11
PrincipalSource : Unknown
ObjectClass : Group
Name : NT AUTHORITY\INTERACTIVE
SID : S-1-5-4
PrincipalSource : Unknown
ObjectClass : Group
Name : MSI\Administrator
SID : S-1-5-21-1457208496-2238809947-3401341024-500
PrincipalSource : Local
ObjectClass : User
Name : MSI\user
SID : S-1-5-21-1457208496-2238809947-3401341024-1001
PrincipalSource : MicrosoftAccount
ObjectClass : User
如果我删除 Get-LocalUser -Name $userName; 调用,我会得到以下信息:
Showing Users group
Showing Administrators group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\user MicrosoftAccount
User MSI\student Local
Group NT AUTHORITY\Authenticated Users Unknown
Group NT AUTHORITY\INTERACTIVE Unknown
User MSI\Administrator Local
User MSI\user MicrosoftAccount
我想要两个单独的对象类列表,中间有空格。就像我手动分别输入每个列表一样,在每个命令后按回车键。看看我在这里试图实现什么:
Showing Users group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\user MicrosoftAccount
User MSI\Student Local
Group NT AUTHORITY\Authenticated Users Unknown
Group NT AUTHORITY\INTERACTIVE Unknown
Showing Administrators group
ObjectClass Name PrincipalSource
----------- ---- ---------------
User MSI\Administrator Local
User MSI\user MicrosoftAccount
Showing local users
Name Enabled Description
---- ------- -----------
student True
答案1
只需通过附加以下内容将所有 Powershell 对象转换为字符串| Out-String
:
$userName = "Student"
Write-Host "`nShowing local user`n"
Get-LocalUser -Name $userName | Out-String;
Write-Host "`nShowing Users group`n";
Get-LocalGroupMember -Group "Users" | Out-String;
Write-Host "`nShowing Administrators group`n";
Get-LocalGroupMember -Group "Administrators" | Out-String;
Write-Host "`nShowing local users`n"
Get-LocalUser -Name $userName | Out-String;
这样做将会给你你想要的输出。