I have two scripts. I would like to take the 1st script and add a condition that if the hostname IP matches a ScopeIP to add the name of that scope to Results.csv
This is what I want my output to look like
I have tried Importing the two output csv files, then comparing, but I don't want to output more than one csv file. It also seems too complicated to reimport the two csv files, then compare the two the ScopeID columns and then get the corresponding scope name from a separate column.
Thank you.
My two scripts:
$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
$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
}
}
$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
}
}
}
}