Need Powershell Scripting Help

Welcome Forums General PowerShell Q&A Need Powershell Scripting Help

Viewing 4 reply threads
  • Author
    Posts
    • #44727
      Participant
      Topics: 1
      Replies: 2
      Points: 0
      Rank: Member

      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.

    • #44738
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      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
      Participant
      Topics: 1
      Replies: 2
      Points: 0
      Rank: Member

      Thanks I understand the issue now.

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

      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
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      @{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
      Participant
      Topics: 1
      Replies: 2
      Points: 0
      Rank: Member

      Worked perfect!!!

      Thanks

      I will check ot the PowerShell lunch training.

      Tim

Viewing 4 reply threads
  • The topic ‘Need Powershell Scripting Help’ is closed to new replies.