Author Posts

June 24, 2016 at 5:06 pm

I have modified a script I found on the internet and everything works great except getting the DNS Server search order. It outputs to an excel file using the Export-Excel module and under the DNSServerSearchOrder Heading in the spreadsheet it shows System.String[]. If I run the command from the command prompt it outputs the DNS servers correctly. Below is the code, any help would be appreciated.

#This Script will find the System Info for local and remote computers.  
# The output displays Computername,IPAddress,NetworkAdapter,MACAddress,DHCPServer,OS,ServicePack, ComputerModel,Username,Domain,Uptime, TotalMem, FreePhysicalMem, RegisteredUser,NoOfProcessors, ProcessorName,ProcessorType 
 
#create an Empty Array 
$array= @() 
 
$computer=Get-Content .\computers.txt 
foreach ($server in $computer) 
{ 
 
$IP=[System.net.dns]::GetHostEntry($server).AddressList | %{$_.IPAddressToString} 
 
$wmi=Get-WmiObject win32_ComputerSystem -ComputerName $server 
 
 
$obj=New-Object PSObject 
 
$obj |Add-Member -MemberType NoteProperty -Name "ComputerName" $wmi.Name 
$obj |Add-Member -MemberType NoteProperty -Name "ComputerModel" $wmi.Model 
$obj |Add-Member -MemberType NoteProperty -Name "Username" $wmi.UserName 
$obj |Add-Member -MemberType NoteProperty -Name "Domain" $wmi.domain 
$obj |Add-Member -MemberType NoteProperty -Name "IPAddress" $IP 
$obj |Add-Member -MemberType NoteProperty -Name "NoOfProcessors" $wmi.Numberofprocessors 
#$array +=$obj 
 
$wmi=Get-WmiObject win32_Processor -ComputerName $server 
 
$obj |Add-Member -MemberType NoteProperty -Name "ProcessorName" $wmi.Name 
$obj |Add-Member -MemberType NoteProperty -Name "ProcessorType" $wmi.Caption 
# $array +=$obj 
 
 
 
$wmi=Get-WmiObject win32_OperatingSystem -ComputerName $server 
 
$freeMB = [math]::Round(($wmi.FreePhysicalMemory / 1024) , 0) 
$totalMB = [math]::Round(($wmi.TotalVisibleMemorySize / 1024) , 0) 
 
$UDays = ((Get-Date) - ($wmi.ConvertToDateTime($wmi.LastBootUpTime))).Days 
$UHours = ((Get-Date) - ($wmi.ConvertToDateTime($wmi.LastBootUpTime))).Hours 
$UMins = ((Get-Date) - ($wmi.ConvertToDateTime($wmi.LastBootUpTime))).Minutes 
$Uptime = "Days " + $UDays + " Hours " + $UHours + " Minutes " + $UMins 
 
$obj |Add-Member -MemberType NoteProperty -Name "OS" $wmi.caption 
$obj |Add-Member -MemberType NoteProperty -Name "Uptime" $Uptime 
$obj |Add-Member -MemberType NoteProperty -Name "RegisteredUser" $wmi.RegisteredUser 
$obj |Add-Member -MemberType NoteProperty -Name "ServicePack" $wmi.csdversion 
$obj |Add-Member -MemberType NoteProperty -Name "FreePhysicalMem" $freeMB 
$obj |Add-Member -MemberType NoteProperty -Name "TotalMem" $totalMB 
 
# Finding the Network Adapter and MAC Address, DHCP Server 
 
$wmi=Get-WmiObject win32_networkadapterconfiguration -ComputerName $server | where {$_.Ipenabled -Match "True"} 
 
$obj |Add-Member -MemberType NoteProperty -Name "NetworkAdapter" $wmi.description 
$obj |Add-Member -MemberType NoteProperty -Name "MACAddress" $wmi.macaddress 
$obj |Add-Member -MemberType NoteProperty -Name "DHCPServer" $wmi.DHCPServer
$obj |Add-Member -MemberType NoteProperty -Name "DNSServerSearchOrder" $wmi.DNSServerSearchOrder
 



$wmi=get-WmiObject win32_logicaldisk -ComputerName $server -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace

$obj | Add-Member -MemberType NoteProperty -Name "SystemDiskSize/GB" ($wmi.size /1Gb)
$obj | Add-Member -MemberType NoteProperty -Name "SystemFreeDiskSpace/GB" ($wmi.FreeSpace / 1GB)


$array +=$obj

} 
 
 
$array | select Computername,Domain,IPAddress,OS,ServicePack,Uptime,ComputerModel,NetworkAdapter,DNSServerSearchOrder,DHCPServer,MACAddress,SystemDiskSize/GB,SystemFreeDiskSpace/GB,TotalMem, FreePhysicalMem, RegisteredUser,Username,NoOfProcessors, ProcessorName,ProcessorType | Export-excel .\systeminfo.xlsx  

June 24, 2016 at 5:30 pm

Pretty common "gotcha." https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/a-crowd-isnt-an-individual.html has some background.

Essentially, you've got an array, and there's no way for an array (multiple values) to go into a single cell without some effor on your part. For example, from your line 75:

$array | select Computername,Domain,IPAddress,OS,ServicePack,Uptime,ComputerModel,NetworkAdapter,@{n='DNSServerSearchOrder',e={$_.DNSServerSearchOrder -join ","}},DHCPServer,MACAddress,SystemDiskSize/GB,SystemFreeDiskSpace/GB,TotalMem, FreePhysicalMem, RegisteredUser,Username,NoOfProcessors, ProcessorName,ProcessorType | Export-excel .\systeminfo.xlsx

What I've done there will turn the array (designated by the [] after String in the output you were seeing) into a comma-seperated set of values. E.g., a single string, which can go into a single cell. You may need to mess with that until you're happy with the output, but it's the general idea.

June 24, 2016 at 5:55 pm

Thanks I understand the issue now.

I copied your code into the script and here are the formatting errors I recieved:

$array | select Computername,Domain,IPAddress,OS,ServicePack,Uptime,ComputerModel,NetworkAdapter,@{n='DNSServerSearchOrder',e={$_.DNSServerSearchOrder -join ","}},DHCPServer,MACAddress,SystemDiskSize/GB,SystemFreeDiskSpace/GB,TotalMem, FreePhysicalMem, RegisteredUser,Username,NoOfProcessors, ProcessorName,ProcessorType | Export-excel .\systeminfo.xlsx

The e= is underlined with the errors

Missing expression after ','
Unexpected token 'e=' in expression
the hash literal was incomplete

The second close parenthesis after -join "," is underlined with the error

unexpected token '}' in expression

DHCPServer is underlined with the error

unexpected token 'dhcpserver' in expression

I have tried to fix formatting with no luck.

Thanks in advance
Tim

June 24, 2016 at 7:04 pm

@{n='DNSServerSearchOrder',e={$_.

should be

@{n='DNSServerSearchOrder';e={$_.

I believe. This technique – called a custom property or calculated property – is included in the examples in the help file for Select-Object, if you'd like to examine that documentation for further examples of its usage. It's also covered fairly extensively in "Learn Windows PowerShell in a Month of Lunches," if you've an interest in really nailing the fundamentals.

June 24, 2016 at 9:45 pm

Worked perfect!!!

Thanks

I will check ot the PowerShell lunch training.

Tim