powershell 中的输出苦苦挣扎
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
}
}
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
}
}
Output now:
Expected output.... every user may be belowe another and the other things same....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您不断覆盖
Process
块内的$PCInfo
值,然后在最后输出最后分配的值。完全删除对
$PCInfo
的赋值,并在创建后立即输出对象。您可能还想显式传递$computer
作为Get-CimInstance
的远程计算机名称进行查询:要将帐户写入文件,只需通过管道传输函数的输出即可到
Export-Csv
或类似的:Right now, you keep overwriting the value of
$PCInfo
inside theProcess
block, and then at the very end you output whatever value was last assigned.Remove the assignment to
$PCInfo
completely, and just output the objects immediately once created. You probably also want to explicitly pass$computer
as the remote computer name forGet-CimInstance
to query:To write the accounts to a file, simply pipe the output from your function to
Export-Csv
or similar: