【问题标题】:Powershell: Exporting AD MachineName/UserName/IPv4Address to a single *.csvPowershell:将 AD MachineName/UserName/IPv4Address 导出到单个 *.csv
【发布时间】:2017-06-28 02:41:18
【问题描述】:

我有一个OU,里面全是默认命名的机器。问题是这些机器已经通过 50 多个站点发送出去,我不知道谁有什么。下面的代码已经完成了这项工作,但我必须合并两个 CSV,这并不是那么复杂,但这是我认为我必须拥有的一个步骤。

这是我当前的代码(使用两个 CSV):

###Creates temp-function to get the current user logged into machine###

function Get-LoggedOnUser {
    [CmdletBinding()]
    param(
        [Parameter()]
        [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )

    foreach ($comp in $ComputerName) {
        $output = @{ 'ComputerName' = $comp }
        $output.UserName = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp).UserName
        [PSCustomObject]$output
    }
}

###Change the -SearchBase parameters to the appropriate container.#######

$computer = Get-ADComputer -Filter * -SearchBase "OU=IMAGE,OU=WORKSTATIONS,DC=AD,DC=XXX,DC=US" |
    Sort-Object Name

$allinfo = @()
foreach ($machine in $computer.Name) {
    $Array = "" | Select-Object Machine
    $array.Machine = $machine
    Get-LoggedOnUser -ComputerName $machine |
        Export-Csv "C:\Users\XXX\Desktop\loggedOnUsers.csv" -NoTypeInformation -Append

    Get-LoggedOnUser -ComputerName $machine |
        Test-Connection -Count 1 |
        Select-Object @{ n = "Machine"; e = { $_.Address } }, Ipv4Address |
        Export-Csv "C:\Users\XXXX\Desktop\ips.csv" -NoTypeInformation -Append
}

我在某些地方用XXX 编辑了代码,我不是在质疑这些区域。我似乎无法将代码结尾合并为一个 CSV。

任何帮助将不胜感激。

【问题讨论】:

    标签: powershell csv active-directory windows-10


    【解决方案1】:

    我建议将 IP 地址添加到您的函数对象,如下所示:

    function Get-LoggedOnUser {
        [CmdletBinding()]
        param(
            [Parameter()]
            [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1})]
            [ValidateNotNullOrEmpty()]
            [string[]]$ComputerName = $env:COMPUTERNAME
        )
    
        foreach ($comp in $ComputerName) {
            $output = @{ 'ComputerName' = $comp }
            $output.UserName = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp).UserName
            $output.Ipv4Address = (Test-Connection -ComputerName $machine -Count 1 | Select-Object -ExpandProperty Ipv4Address)
            [PSCustomObject]$output
        }
    }
    
    ###Change the -SearchBase parameters to the appropriate container.#######
    
    $computer = Get-ADComputer -Filter * -SearchBase "OU=IMAGE,OU=WORKSTATIONS,DC=AD,DC=XXX,DC=US" |
    Sort-Object Name
    
    $allinfo = @()
    foreach ($machine in $computer.Name) {
        $Array = "" | Select-Object Machine
        $array.Machine = $machine
    
    Get-LoggedOnUser -ComputerName $machine | Export-Csv "C:\Users\XXX\Desktop\\AllInOne.csv" -NoTypeInformation -Append
    }
    

    【讨论】:

    • 非常感谢!这正是我所缺少的。我曾尝试在错误的位置创建一个变量!
    猜你喜欢
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2020-07-03
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多