Powershell:将 AD 计算机名称/用户名/IPv4 地址导出到单个 *.csv



我有一个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:UsersXXXDesktoploggedOnUsers.csv" -NoTypeInformation -Append
    Get-LoggedOnUser -ComputerName $machine |
        Test-Connection -Count 1 |
        Select-Object @{ n = "Machine"; e = { $_.Address } }, Ipv4Address |
        Export-Csv "C:UsersXXXXDesktopips.csv" -NoTypeInformation -Append
}

我已经在某些地方用XXX编辑了代码,我不是在质疑这些领域。我似乎无法将代码的末尾合并到一个 CSV 中。

任何帮助将不胜感激。

我建议像这样将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:UsersXXXDesktop\AllInOne.csv" -NoTypeInformation -Append
}

最新更新