Author Posts

January 1, 2012 at 12:00 am

by sandheepkrishna at 2012-09-10 14:44:29

I wish you could help me on this powershell script error.
Im using powershell to write some scripts, in between i come across an issue which i cannot find any solution so far.

Below is my script, error is followed by the script

$IPData= Get-WmiObject -ComputerName $_.DNSHostName -EnableAllPrivileges:$true -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | select DNSHostname,@{Name=’IPAddress’;Expression={[string]::join(";", ($_.IPAddress))}},@{Name=’DefaultGeteway’;Expression={[string]::join(";", ($_.DefaultIPGateway))}}, @{Name=’SubnetMask’;Expression={[string]::join(";", ($_.IPSubnet))}},@{Name="DNSDomainSuffixSearchOrder";Expression={[string]]::Join(";",($_.DNSServerSearchOrder))}}, MACAddress, Description, Caption, DeadGWDetectEnabled,ServiceName,DomainDNSRegistrationEnabled,FullDNSRegistrationEnabled

\$result= New-Object -TypeName PSObject
if ($IPData.count -gt 0)
{$i=0
while ($i -lt $IPData.count)
{
$result | Add-Member -MemberType NoteProperty -Name IPs -value $IPData[$i].IPAddress
$result | Add-Member -MemberType NoteProperty -Name GW -value $IPData[$i].DefaultGeteway
$result | Add-Member -MemberType NoteProperty -Name IPMask -value $IPData[$i].SubnetMask
$result | Add-Member -MemberType NoteProperty -Name DNSServer -value $IPData[$i].DNSServerSearchOrder
$result | Add-Member -MemberType NoteProperty -Name DNSSuffix -value $IPData[$i].DNSDomainSuffixSearchOrder
$result | Add-Member -MemberType NoteProperty -Name MAC -value $IPData[$i].MACAddress
$result | Add-Member -MemberType NoteProperty -Name NWCardDes -value $IPData[$i].Description
$result | Add-Member -MemberType NoteProperty -Name NWCardCaption -value $IPData[$i].Caption
$result | Add-Member -MemberType NoteProperty -Name DeadGWDetection -value $IPData[$i].DeadGWDetectEnabled
$result | Add-Member -MemberType NoteProperty -Name ServiceName -value $IPData[$i].ServiceName
$result | Add-Member -MemberType NoteProperty -Name DomainDNSRegistrationEnabled -value $IPData[$i].DomainDNSRegistrationEnabled
$result | Add-Member -MemberType NoteProperty -Name FullDNSRegistrationEnabled -value $IPData[$i].FullDNSRegistrationEnabled
$export+= $result
$i= $i+1
}
}
else
{
$result | Add-Member -MemberType NoteProperty -Name IPs -value $IPData.IPAddress
$result | Add-Member -MemberType NoteProperty -Name GW -value $IPData.DefaultGeteway
$result | Add-Member -MemberType NoteProperty -Name IPMask -value $IPData.SubnetMask
$result | Add-Member -MemberType NoteProperty -Name DNSServer -value $IPData.DNSServerSearchOrder
$result | Add-Member -MemberType NoteProperty -Name DNSSuffix -value $IPData.DNSDomainSuffixSearchOrder
$result | Add-Member -MemberType NoteProperty -Name MAC -value $IPData.MACAddress
$result | Add-Member -MemberType NoteProperty -Name NWCardDes -value $IPData.Description
$result | Add-Member -MemberType NoteProperty -Name NWCardCaption -value $IPData.Caption
$result | Add-Member -MemberType NoteProperty -Name DeadGWDetection -value $IPData.DeadGWDetectEnabled
$result | Add-Member -MemberType NoteProperty -Name ServiceName -value $IPData.ServiceName
$result | Add-Member -MemberType NoteProperty -Name DomainDNSRegistrationEnabled -value $IPData.DomainDNSRegistrationEnabled
$result | Add-Member -MemberType NoteProperty -Name FullDNSRegistrationEnabled -value $IPData.FullDNSRegistrationEnabled
$export+= $result
}
$Export | select * | export-csv report.csv -notype

And the error is ....

Add-Member : Cannot add a member with the name "GW" because a member with that name already exists. If you want to over
write the member anyway, use the Force parameter to overwrite it.
At Z:\get-computerinfo.ps1:56 char:24
+ $result | Add-Member < <<< -MemberType NoteProperty -Name GW -value $IPData[$i].DefaultGeteway
+ CategoryInfo : InvalidOperation: (@{Name=ITSMS81M...onEnabled=True}:PSObject) [Add-Member], InvalidOper
ationException
+ FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand

I really wish if you could help me to fix this.

by DonJ at 2012-09-10 15:25:43

You're only creating the new object once, at the start of your script. You're never re-creating it. Move the New-Object to just before you start adding members:


if ($IPData.count -gt 0)
{$i=0
while ($i -lt $IPData.count)
{
$result= New-Object -TypeName PSObject
$result | Add-Member -MemberType NoteProperty -Name IPs -value $IPData[$i].IPAddress
$result | Add-Member -MemberType NoteProperty -Name GW -value $IPData[$i].DefaultGeteway
$result | Add-Member -MemberType NoteProperty -Name IPMask -value $IPData[$i].SubnetMask
$result | Add-Member -MemberType NoteProperty -Name DNSServer -value $IPData[$i].DNSServerSearchOrder
$result | Add-Member -MemberType NoteProperty -Name DNSSuffix -value $IPData[$i].DNSDomainSuffixSearchOrder
$result | Add-Member -MemberType NoteProperty -Name MAC -value $IPData[$i].MACAddress
$result | Add-Member -MemberType NoteProperty -Name NWCardDes -value $IPData[$i].Description
$result | Add-Member -MemberType NoteProperty -Name NWCardCaption -value $IPData[$i].Caption
$result | Add-Member -MemberType NoteProperty -Name DeadGWDetection -value $IPData[$i].DeadGWDetectEnabled
$result | Add-Member -MemberType NoteProperty -Name ServiceName -value $IPData[$i].ServiceName
$result | Add-Member -MemberType NoteProperty -Name DomainDNSRegistrationEnabled -value $IPData[$i].DomainDNSRegistrationEnabled
$result | Add-Member -MemberType NoteProperty -Name FullDNSRegistrationEnabled -value $IPData[$i].FullDNSRegistrationEnabled
$export+= $result
$i= $i+1
}
}
else
{
$result= New-Object -TypeName PSObject
$result | Add-Member -MemberType NoteProperty -Name IPs -value $IPData.IPAddress
$result | Add-Member -MemberType NoteProperty -Name GW -value $IPData.DefaultGeteway
$result | Add-Member -MemberType NoteProperty -Name IPMask -value $IPData.SubnetMask
$result | Add-Member -MemberType NoteProperty -Name DNSServer -value $IPData.DNSServerSearchOrder
$result | Add-Member -MemberType NoteProperty -Name DNSSuffix -value $IPData.DNSDomainSuffixSearchOrder
$result | Add-Member -MemberType NoteProperty -Name MAC -value $IPData.MACAddress
$result | Add-Member -MemberType NoteProperty -Name NWCardDes -value $IPData.Description
$result | Add-Member -MemberType NoteProperty -Name NWCardCaption -value $IPData.Caption
$result | Add-Member -MemberType NoteProperty -Name DeadGWDetection -value $IPData.DeadGWDetectEnabled
$result | Add-Member -MemberType NoteProperty -Name ServiceName -value $IPData.ServiceName
$result | Add-Member -MemberType NoteProperty -Name DomainDNSRegistrationEnabled -value $IPData.DomainDNSRegistrationEnabled
$result | Add-Member -MemberType NoteProperty -Name FullDNSRegistrationEnabled -value $IPData.FullDNSRegistrationEnabled
$export+= $result
}
$Export | select * | export-csv report.csv -notype

by sandheepkrishna at 2012-09-10 15:37:16

When I do that, re-created objects attributes are not exported to csv file. I need all these get exported to a single csv file.

by DonJ at 2012-09-10 15:51:57

Correct. You are going to have to create multiple objects, and they will all start with a blank slate. If you have a set of re-used properties, you can shortcut things like this:


$reused = @{'Property1'='Value1';'Property2'='Value2'}
while ($whatever -lt $that) {
$obj = New-Object -TypeName PSObject -Prop $reused
$obj | Add-Member -MemberType NoteProperty -Name GW -Value $thisvalue
}

The problem with your script is that you can't keep adding the member GW to the object over and over. A member can only be added once. Then you need to create a new, fresh object the next time through the loop. The technique I've shown you lets you set up a group of shared properties, so that those can be quickly added each time you create a new object.

by Helmto108 at 2012-09-30 17:19:08

I ran into a similar type of problem where I had to collect and store user variables. Does the following work for you? You can re-add if the If statement if you still need a conditional.

$export = @()

ForEach ($cpu in $IPData) {

$result = $null
$result = New-Object PSObject

$result | Add-Member -Type NoteProperty -Name IPs -value $cpu.IPAddress
$result | Add-Member -Type NoteProperty -Name GW -value $cpu.DefaultGeteway
$result | Add-Member -Type NoteProperty -Name IPMask -value $cpu.SubnetMask
$result | Add-Member -Type NoteProperty -Name DNSServer -value $cpu.DNSServerSearchOrder
$result | Add-Member -Type NoteProperty -Name DNSSuffix -value $cpu.DNSDomainSuffixSearchOrder
$result | Add-Member -Type NoteProperty -Name MAC -value $cpu.MACAddress
$result | Add-Member -Type NoteProperty -Name NWCardDes -value $cpu.Description
$result | Add-Member -Type NoteProperty -Name NWCardCaption -value $cpu.Caption
$result | Add-Member -Type NoteProperty -Name DeadGWDetection -value $cpu.DeadGWDetectEnabled
$result | Add-Member -Type NoteProperty -Name ServiceName -value $cpu.ServiceName
$result | Add-Member -Type NoteProperty -Name DomainDNSRegistrationEnabled -value $cpu.DomainDNSRegistrationEnabled
$result | Add-Member -Type NoteProperty -Name FullDNSRegistrationEnabled -value $cpu.FullDNSRegistrationEnabled

$export += $result

}

$Export | Out-file report.csv

by RichardSiddaway at 2012-10-01 10:35:28

You can simplify your script in a number of ways. Your select statement is doing unnecessary work and you don't maximise the use of the pipeline. try something like this
$computer = $env:COMPUTERNAME
$exportdata = @()

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = $true" `
-ErrorAction SilentlyContinue -WarningAction SilentlyContinue |
foreach {
$props = [ordered]@{
DNSHostName = $($_.DNSHostName)
IPs = $($_.IPAddress -join ",")
GW = $($_.DefaultIPGateway -join ",")
IPMask = $($_.IPSubnet-join ",")
DNSServer = $($_.DNSServerSearchOrder -join ",")
DNSSuffix = $($_.DNSDomainSuffixSearchOrder -join ",")
MAC = $($_.MACAddress)
NWCardDes = $($_.Description)
NWCardCaption = $($_.Caption)
DeadGWDetection = $($_.DeadGWDetectEnabled)
ServiceName = $($_.ServiceName)
DomainDNSRegistrationEnabled = $($_.DomainDNSRegistrationEnabled)
FullDNSRegistrationEnabled = $($_.FullDNSRegistrationEnabled)
}

$exportdata += New-Object -TypeName PSObject -Property $props
}
$exportdata | Export-Csv Report.Csv -NoTypeInformation

by mjolinor at 2012-10-09 07:56:44

I don't think you need that $exportdata variable.
When there are a lot of properties to define, I find it's helpful to move them outside of the loop logic with a script block. That makes the loop logic easier to read and diagnose (for me, anyway).

$computer = $env:COMPUTERNAME
$exportdata = @()

$props = {[ordered]@{
DNSHostName = $($_.DNSHostName)
IPs = $($_.IPAddress -join ",")
GW = $($_.DefaultIPGateway -join ",")
IPMask = $($_.IPSubnet-join ",")
DNSServer = $($_.DNSServerSearchOrder -join ",")
DNSSuffix = $($_.DNSDomainSuffixSearchOrder -join ",")
MAC = $($_.MACAddress)
NWCardDes = $($_.Description)
NWCardCaption = $($_.Caption)
DeadGWDetection = $($_.DeadGWDetectEnabled)
ServiceName = $($_.ServiceName)
DomainDNSRegistrationEnabled = $($_.DomainDNSRegistrationEnabled)
FullDNSRegistrationEnabled = $($_.FullDNSRegistrationEnabled)
}
}

$GWMI_Params = @{
Class = 'Win32_NetworkAdapterConfiguration'
Filter = "IPEnabled = $true"
ErrorAction = 'SilentlyContinue'
WarningAction = 'SilentlyContinue'
}

Get-WmiObject @GWMI_Params |

foreach {
New-Object -TypeName PSObject -Property (&$props)
} |

Export-Csv Report.Csv -NoTypeInformation

by danpotter at 2012-10-16 08:35:25

here's an example.

function ex-machines{

get-adcomputer -Searchbase "ou=myou,dc=something,dc=com" -properties * -filter * | % {

$net = Get-wmiObject Win32_networkadapterconfiguration -computername $_.name | Where{$_.IpEnabled -eq $true}

foreach($dns in $net){

$ip = [string]$dns.ipaddress
$dns1 = $dns.DNSServerSearchOrder[0]
$dns2 = $dns.DNSServerSearchOrder[1]
$wins1 = $dns.winsprimaryserver
$wins2 = $dns.winssecondaryserver
$dhcp = $dns.dhcpserver

$output = new-object PsObject
$output | add-member NoteProperty "server" -value $_.name
$output | add-member NoteProperty "dn" -value $_.distinguishedname
$output | add-member NoteProperty "os" -value $_.operatingsystem
$output | add-member NoteProperty "ipaddress" -value $ip
$output | add-member NoteProperty "dhcp" -value $dhcp
$output | add-member NoteProperty "primarydns" -value $dns1
$output | add-member NoteProperty "secondarydns" -value $dns2
$output | add-member NoteProperty "winsprimary" -value $wins1
$output | add-member NoteProperty "winssecondary" -value $wins2

$output

$dns1 = $null
$dns2 = $null
$wins1 = $null
$wins2 = $null

}

}

}

ex-machines | export-csv machines.csv