Answered by:
Export scripted info and then turn around and import info to another script

Question
-
Hello,
I am trying to retrieve a list of servers on a subnet and then verify if they have a dhcp reservation.
I've found 2 scripts online that do what I need to do, I just need to tie them together.
I'm having trouble exporting the server names gathered from the first script and importing them into the second script. It may be that I can combine them with commands in the middle of the two.
I'd like to export the info found in this script, which is the server names in my domain:
$strCategory = "computer" $strOperatingSystem = "Windows*Server*" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = ("OperatingSystem=$strOperatingSystem") $colProplist = "name" foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() foreach ($objResult in $colResults) { $objComputer = $objResult.Properties; $objComputer.name }
I'd then like to import the exported information to be included in this script to see if there is a reservation for the servers:
$ComputerName = (Imported names from above) $NetAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName | ? {$_.DHCPEnabled -eq $True -and $null -ne $_.IPAddress} If ($NetAdapters) { Foreach ($Adapter in $NetAdapters) { foreach ($IP in $Adapter.IPAddress) { $Reservation = Get-DhcpServerv4Reservation -ScopeId $IP -ComputerName $Adapter.DHCPServer | ? {$_.ScopeId -eq $_.IPAddress} If ($Reservation) { Write-Output "$IP is reserved on $($Adapter.DHCPServer)." } Else { Write-Output "$IP does not have a reservation." } } } } Else { Write-Output "No DHCP Enabled NetAdapters with IPAddresses exist on host, likely Static" }
Thanks for any assistance.
- Moved by Bill_Stewart Wednesday, May 9, 2018 2:44 PM This is not "scripts on demand"
Wednesday, February 28, 2018 4:04 PM
Answers
-
Get the first script output into the variable that you want to use in the second script. Do this by processing the names in a loop.
Look at the learning links to help you learn how to use PowerShell.
Also this is a good resource: https://technet.microsoft.com/en-us/scriptcenter/default.aspx
\_(ツ)_/
- Marked as answer by FlynnTKeilty Thursday, January 17, 2019 9:46 PM
Wednesday, February 28, 2018 4:16 PM
All replies
-
Please carefully review the following links to set your expectation of technical forums.
This Forum is for Scripting Question Rather than script requests
From a Bill Stewart summary of useful forum links:
- Posting guidelines
- Handy tips for posting to this forum
- How to ask questions in a technical forum
- Rubber duck problem solving
- How to write a bad forum post
- Help Vampires: A Spotter's Guide
- This forum is for scripting questions rather than script requests
\_(ツ)_/
Wednesday, February 28, 2018 4:13 PM -
Get the first script output into the variable that you want to use in the second script. Do this by processing the names in a loop.
Look at the learning links to help you learn how to use PowerShell.
Also this is a good resource: https://technet.microsoft.com/en-us/scriptcenter/default.aspx
\_(ツ)_/
- Marked as answer by FlynnTKeilty Thursday, January 17, 2019 9:46 PM
Wednesday, February 28, 2018 4:16 PM -
In case anyone finds this thread while looking to do what I had wanted to do, here is the script that I came up with after learning more powershell.
Get-ADComputer -Filter {OperatingSystem -like "*Windows Server*"} | select-object Name | export-csv "C:\scripts\servers.csv" -NoTypeInformation import-csv -path "c:\scripts\servers.csv" -Header Name | Select -Skip 1 | Foreach-Object { $NetAdapters = Get-WmiObject Win32_NetworkAdapterConfiguration -computername $_.Name | ? {$_.DHCPEnabled -eq $True -and $null -ne $_.IPAddress} If ($NetAdapters) { Foreach ($Adapter in $NetAdapters) { foreach ($IP in $Adapter.IPAddress -notlike'*:*') { $Reservation = Get-DhcpServerv4Reservation -ScopeId $IP -ComputerName (dhcpserver) | ? {$_.ScopeId -eq $_.IPAddress} If ($Reservation) { Write-Output "$IP is reserved on $($Adapter.dhcpserver)." } Else { Write-Output "$IP does not have a reservation." } } } } Else { Write-Output "No DHCP Enabled NetAdapters with IPAddresses exist on host, likely Static" } } | out-file -FilePath C:\scripts\reservationinfo.csv
Thursday, March 22, 2018 3:52 PM