在 PowerShell 中,结合这两个脚本,如果主机名 IP 与 DHCP 范围匹配,则附加 csv 输出
In PowerShell, combine these two scripts where if the hostname IP matches the DHCP scope, append the csv output
我有两个脚本。我想采用第一个脚本并添加一个条件,如果主机名 IP 与 ScopeIP 匹配,则将该范围的名称添加到 Results.csv
This is my current output
This is what I want my output to look like
我试过导入两个输出的csv文件,然后比较,但我不想输出一个以上的csv文件。重新导入两个 csv 文件,然后比较两个 ScopeID 列,然后从单独的列中获取相应的范围名称似乎也太复杂了。
谢谢。
我的两个脚本:
- 使用输出到 Results.csv
的主机名列表获取客户端信息
$list = Get-Content C:\script\HostNames.txt #Defines content it pulls as list
$Output = foreach ($hostname in $list) #Calls each item in list a hostname and sends to output
{
if (test-connection -count 1 -computername $hostname -quiet) #checking if hostname is on line with 1 ping, If online run the following
{
$System = Get-WmiObject Win32_ComputerSystem -ComputerName $hostname | Select-Object -Property Name,Model
$BIOS = Get-WmiObject Win32_BIOS -ComputerName $hostname | Select-Object -Property SerialNumber
$User = get-childitem "C:\Users" | Sort-Object LastWriteTime -Descending | Select-Object -first 1
$mac = invoke-command -computername $hostname {(gwmi -class win32_networkadapterconfiguration).MacAddress | select -first 1}
$IpV = (test-connection -ComputerName $hostname -count 1 | select -expandproperty IPV4Address).IPaddresstostring
$parts = $IpV.Split(".") #converts the last octet into a zero
$parts[3] = "0"
$ip2 = [String]::Join(".", $parts)
}
else #statement if hostname is not online
{
write-host $hostname not online
}
[PSCustomObject]@{ #Rename varibles in data pull for output file
ComputerName = $hostname
Model = $System.Model
SerialNumber = $BIOS.SerialNumber
LastUser = $User
MacAddress = $mac
IpAddress = $IpV
IpScope = $ip2}
}
$Output
$Output | Export-Csv -Path C:\script\Result.csv -NoTypeInformation
- 获取 DHCP 范围名称和范围输出到 ServerScopes.csv
$DHServers = Get-DhcpServerInDC
foreach ($Server in $DHServers)
{
$scopes = Get-DHCPServerv4Scope -ComputerName $Server.DnsName | Select-Object Name, ScopeID #only getting the Name and ScopeID
ForEach ($Address in $scopes)
{
$DHCPServer = $Server.DnsName
$Address | Export-Csv "C:\script\Results\ServerScopes.csv" -Append -NoTypeInformation
}
}
Here is what my DHCP scope names output looks like
处理此问题的一种方法是获取第二个 CSV 数据并将其转换为散列 table。当然,我会在导出数据之前处理数据(或者仍然可以使用)但是从你的例子开始......
# Create a lookup table where the key is the ScopeID
$lookup = Import-Csv -Path "C:\script\Results\ServerScopes.csv" | Group-Object -Property ScopeID -AsHashTable
然后在您的第一个脚本中像这样在您的对象创建中添加一行
[PSCustomObject]@{ #Rename varibles in data pull for output file
ComputerName = $hostname
Model = $System.Model
SerialNumber = $BIOS.SerialNumber
LastUser = $User
MacAddress = $mac
IpAddress = $IpV
IpScope = $ip2
Name = $lookup[$ip2].name
}
$DHServers = Get-DhcpServerInDC #get DHCP info
$hashtable = @{} #create hash table
foreach ($server in $DHServers){
$scopes = Get-DHCPServerv4Scope -ComputerName $server.dnsname #get all scopes in DHCP
foreach ($_ in (Import-Csv C:\script\Asset_List.csv | Select-Object -ExpandProperty asset)){ #get hostnames from list
foreach ($scope in $scopes){
if($scope | Get-DhcpServerV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$_*" ){ #compares the hostname to find which lease it is in
$scopename=$scope.name #matches give scope name
$hashtable[$_] = $scopename #when a match is found, add keys and values to table
}
}
}
}
我有两个脚本。我想采用第一个脚本并添加一个条件,如果主机名 IP 与 ScopeIP 匹配,则将该范围的名称添加到 Results.csv
This is my current output
This is what I want my output to look like
我试过导入两个输出的csv文件,然后比较,但我不想输出一个以上的csv文件。重新导入两个 csv 文件,然后比较两个 ScopeID 列,然后从单独的列中获取相应的范围名称似乎也太复杂了。
谢谢。
我的两个脚本:
- 使用输出到 Results.csv 的主机名列表获取客户端信息
$list = Get-Content C:\script\HostNames.txt #Defines content it pulls as list
$Output = foreach ($hostname in $list) #Calls each item in list a hostname and sends to output
{
if (test-connection -count 1 -computername $hostname -quiet) #checking if hostname is on line with 1 ping, If online run the following
{
$System = Get-WmiObject Win32_ComputerSystem -ComputerName $hostname | Select-Object -Property Name,Model
$BIOS = Get-WmiObject Win32_BIOS -ComputerName $hostname | Select-Object -Property SerialNumber
$User = get-childitem "C:\Users" | Sort-Object LastWriteTime -Descending | Select-Object -first 1
$mac = invoke-command -computername $hostname {(gwmi -class win32_networkadapterconfiguration).MacAddress | select -first 1}
$IpV = (test-connection -ComputerName $hostname -count 1 | select -expandproperty IPV4Address).IPaddresstostring
$parts = $IpV.Split(".") #converts the last octet into a zero
$parts[3] = "0"
$ip2 = [String]::Join(".", $parts)
}
else #statement if hostname is not online
{
write-host $hostname not online
}
[PSCustomObject]@{ #Rename varibles in data pull for output file
ComputerName = $hostname
Model = $System.Model
SerialNumber = $BIOS.SerialNumber
LastUser = $User
MacAddress = $mac
IpAddress = $IpV
IpScope = $ip2}
}
$Output
$Output | Export-Csv -Path C:\script\Result.csv -NoTypeInformation
- 获取 DHCP 范围名称和范围输出到 ServerScopes.csv
$DHServers = Get-DhcpServerInDC
foreach ($Server in $DHServers)
{
$scopes = Get-DHCPServerv4Scope -ComputerName $Server.DnsName | Select-Object Name, ScopeID #only getting the Name and ScopeID
ForEach ($Address in $scopes)
{
$DHCPServer = $Server.DnsName
$Address | Export-Csv "C:\script\Results\ServerScopes.csv" -Append -NoTypeInformation
}
}
Here is what my DHCP scope names output looks like
处理此问题的一种方法是获取第二个 CSV 数据并将其转换为散列 table。当然,我会在导出数据之前处理数据(或者仍然可以使用)但是从你的例子开始......
# Create a lookup table where the key is the ScopeID
$lookup = Import-Csv -Path "C:\script\Results\ServerScopes.csv" | Group-Object -Property ScopeID -AsHashTable
然后在您的第一个脚本中像这样在您的对象创建中添加一行
[PSCustomObject]@{ #Rename varibles in data pull for output file
ComputerName = $hostname
Model = $System.Model
SerialNumber = $BIOS.SerialNumber
LastUser = $User
MacAddress = $mac
IpAddress = $IpV
IpScope = $ip2
Name = $lookup[$ip2].name
}
$DHServers = Get-DhcpServerInDC #get DHCP info
$hashtable = @{} #create hash table
foreach ($server in $DHServers){
$scopes = Get-DHCPServerv4Scope -ComputerName $server.dnsname #get all scopes in DHCP
foreach ($_ in (Import-Csv C:\script\Asset_List.csv | Select-Object -ExpandProperty asset)){ #get hostnames from list
foreach ($scope in $scopes){
if($scope | Get-DhcpServerV4Lease -ComputerName $server.dnsname | Where-Object HostName -like "$_*" ){ #compares the hostname to find which lease it is in
$scopename=$scope.name #matches give scope name
$hashtable[$_] = $scopename #when a match is found, add keys and values to table
}
}
}
}