Need Powershell Scripting Help

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Tim Pillow Tim Pillow 5 months, 1 week ago.

  • Author
    Posts
  • #44727
    Profile photo of Tim Pillow
    Tim Pillow
    Participant

    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  
    
  • #44738
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #44757
    Profile photo of Tim Pillow
    Tim Pillow
    Participant

    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

  • #44764
    Profile photo of Don Jones
    Don Jones
    Keymaster

    @{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.

  • #44823
    Profile photo of Tim Pillow
    Tim Pillow
    Participant

    Worked perfect!!!

    Thanks

    I will check ot the PowerShell lunch training.

    Tim

You must be logged in to reply to this topic.