powershell 中的输出苦苦挣扎

发布于 2025-01-11 05:12:08 字数 2472 浏览 0 评论 0原文

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:
enter image description here
Expected output.... every user may be belowe another and the other things same....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2025-01-18 05:12:08

现在,您不断覆盖 Process 块内的 $PCInfo 值,然后在最后输出最后分配的值。

完全删除对$PCInfo的赋值,并在创建后立即输出对象。您可能还想显式传递 $computer 作为 Get-CimInstance 的远程计算机名称进行查询:

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 {
        $NotReachableComputers = @()
    }

    Process {
        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 | ForEach-Object {
                    # Don't assign this object to a variable, just let it "bubble up" to the caller
                    [PSCustomObject]@{
                        ComputerName     = $computer
                        Name             = $_.Name
                        SID              = $_.SID
                        Lockout          = $_.Lockout
                        Disabled         = $_.Disabled
                        LocalAdminMember = $_.LocalAdminMember 
                    }
                }
            }
            else {
                # Keep track of unreachable machines still
                $NotReachableComputers += $computer
            }
        }
    }

    End {
        if ($NotReachableComputers.Count -ge 1) {
            Write-Host "This system is not available on network" -ForegroundColor Red
            Write-Host $NotReachableComputers
        }
    }
}

要将帐户写入文件,只需通过管道传输函数的输出即可到 Export-Csv 或类似的:

Get-PCinfo -ComputerName pc01,pc02,pc03 |Export-Csv path\to\output.csv -NoTypeInformation

Right now, you keep overwriting the value of $PCInfo inside the Process 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 for Get-CimInstance to query:

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 {
        $NotReachableComputers = @()
    }

    Process {
        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 | ForEach-Object {
                    # Don't assign this object to a variable, just let it "bubble up" to the caller
                    [PSCustomObject]@{
                        ComputerName     = $computer
                        Name             = $_.Name
                        SID              = $_.SID
                        Lockout          = $_.Lockout
                        Disabled         = $_.Disabled
                        LocalAdminMember = $_.LocalAdminMember 
                    }
                }
            }
            else {
                # Keep track of unreachable machines still
                $NotReachableComputers += $computer
            }
        }
    }

    End {
        if ($NotReachableComputers.Count -ge 1) {
            Write-Host "This system is not available on network" -ForegroundColor Red
            Write-Host $NotReachableComputers
        }
    }
}

To write the accounts to a file, simply pipe the output from your function to Export-Csv or similar:

Get-PCinfo -ComputerName pc01,pc02,pc03 |Export-Csv path\to\output.csv -NoTypeInformation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文