I am new to powershell....like a week into it.
I have been asked to query all servers for IP info. Found a great script (Get-IPDetails.ps1) written by
My company has servers all over the world in 8 domains. When I run this command:
get-content "C:\scripts\CAP servers.txt" | .\Get-IPDetails.ps1 | export-csv C:\scripts\output.csv
I get the error Get-WmiObject: Access is denied on many of the servers
On the output.csv file each of those entries will have the ip details for the last successful entry
Example: (When running the command I saw Server2 received "Access Denied" during the process)
ComputerName IPAddress
Server1 10.10.10.10 (ping verified correct)
Server2 10.10.10.10 (ping verified incorrect)
Server3 10.128.20.5 (ping verified correct)
How do I tell the script not to output data for any server returning the "Access is denied" error?
Thanks.
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}
end {}