function Get-PCinfo {
<#
.Synopsis
Short description
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.NOTES
<Zadanie 5>
<Author email>
#>
[CmdletBinding()]
Param(
# Param1 help description
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName = $env:COMPUTERNAME
## Param2 help description
#[int]
#[switch]$outFile = $false
)
Begin{
$Info = @()
$Info | Format-List
}
Process{
foreach ($computer in $ComputerName) {
$NotReachableComputers=$null
Write-Host "Testing connectivity $computer ..... please wait" -ForegroundColor White
if (Test-Connection $computer -Quiet -Count 1) {
Get-CimInstance win32_UserAccount | ForEach-Object {
$PCInfo = [PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Name = [string](Get-CimInstance win32_UserAccount).Name
SID = [string](Get-CimInstance win32_UserAccount).SID
Lockout = [string](Get-CimInstance win32_UserAccount).Lockout
Disabled = [string](Get-CimInstance win32_UserAccount).Disabled
LocalAdminMember= $_.LocalAdminMember }
}
}
else {
$NotReachableComputers += $computer.name
}
}
}
End{
if($NotReachableComputers -ne $null){
Write-Host "This system is not available on network" -ForegroundColor Red
Write-Host $NotReachableComputers
}
else
{
Write-Host "Code worked on all PCs" -ForegroundColor Green
Write-Output $PCInfo
}
#List of systems that were not available on the network
#List of output from systems that were available
#List where the output file is located
}
}
我的问题是输出全部在一起,但我想要单独输出
所以我的输出看起来像这样
或者像这样
我想要的是一张像这样的桌子
ComputerName Name SID .....
PCname Name1 SID1 ......
PCname Name2 SID2.....
答案1
你的函数永远不会-ComputerName $computer
为你的Get-CimInstance
命令指定。这意味着它仅有的从未在本地机器上运行过。尝试像这样更新它:
foreach ($computer in $ComputerName) {
if (Test-Connection $computer -Quiet -Count 1) {
Get-CimInstance win32_UserAccount -ComputerName $computer
该函数未指定您想要在 内包含哪些用户的详细信息$PCInfo
。您也不需要多次查询,因为您的查询对于每个用户的属性都是相同的。Select-Object
而是使用 来包含多个属性。此外,您可以通过仅使用 过滤本地帐户来加快该过程-Filter "LocalAccount=True"
:
$PCInfo = foreach ($computer in $ComputerName) {
Write-Host "Testing connectivity $computer ..... please wait" -ForegroundColor White
if (Test-Connection $computer -Quiet -Count 1) {
Get-CimInstance win32_UserAccount -ComputerName $computer -Filter "LocalAccount=True" |
Select-Object Name,SID,Lockout,Disabled,LocalAdminMember
}
}
Write-Output $PCInfo
另外还有两点:
Win32_UserAccount
对象不包含名为 的属性LocalAdminMember
。您需要通过其他方式查找该信息。Test-Connection $computer
$computer
本地机器何时失败
答案2
#1 PSSession 对此更合适。
#2 你的问题在这里:
Get-CimInstance win32_UserAccount | ForEach-Object {
$PCInfo = [PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Name = [string](Get-CimInstance win32_UserAccount).Name
SID = [string](Get-CimInstance win32_UserAccount).SID
Lockout = [string](Get-CimInstance win32_UserAccount).Lockout
Disabled = [string](Get-CimInstance win32_UserAccount).Disabled
LocalAdminMember= $_.LocalAdminMember }
}
像这样运行此命令只会运行“Get-CimInstance win32_UserAccount”4 次。每次运行命令时,它都会返回一个数组,然后将 .Name 添加到结果 ie(command).Name,从数组中的所有对象中选择属性“Name”,最后将它们存储在新的 PSCustomObject 中的键“Name”中。这就是您获得上述第二个输出的原因。
你应该像这样运行它:
$PCInfo 将被初始化为一个空的 system.array
$PCInfo = @()
Get-CimInstance 将运行一次,然后其结果将通过管道发送到 foreach-object,这将处理每个对象,为处理的单个对象选择属性,然后将其附加到 PCInfo 系统数组(+= 为您完成此操作)
Get-CimInstance win32_UserAccount | ForEach-Object {
$PCInfo += [PSCustomObject]@{
ComputerName = $env:COMPUTERNAME
Name = $_.Name
SID = $_.SID
Lockout = $_.Lockout
Disabled = $_.Disabled
LocalAdminMember= $_.LocalAdminMember }
}
在这之后$PCInfo | 格式表应该以您所寻求的方式提供给您。