gpt4 book ai didi

powershell - Powershell:将AD MachineName/UserName/IPv4Address导出到单个* .csv

转载 作者:行者123 更新时间:2023-12-02 23:41:08 26 4
gpt4 key购买 nike

我的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编辑了代码,我没有对那些 Realm 提出疑问。我似乎无法将代码的结尾合并到一个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:\Users\XXX\Desktop\\AllInOne.csv" -NoTypeInformation -Append
}

关于powershell - Powershell:将AD MachineName/UserName/IPv4Address导出到单个* .csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42147700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com