question

AintGotNoTime-6052 avatar image
0 Votes"
AintGotNoTime-6052 asked RichMatheisen-8856 answered

PSCustomObject to array

I originally build this from examples on the forum, but now I am trying to export to csv and all i get is one line of nonsense. So its my understanding in order to get this to csv file I have to re-import to another array and then export from there. So my ask is there a better way to do this code, from the get go so i done have to re-add to another array just to export. Thank you for the assistance
#Script to troubleshoot Cyberark accounts
Import-Module ActiveDirectory

 $final = @()
    
  # results template
  $output = [ordered]@{
      Hostname = ""
      'RDP_Pass_?' = ""
      'DNS_Resolved_?' = ""
      DName = ""   
  }
        
  #Get the csv file
  $list = Get-Content -Path C:\temp\server.txt
  #Now loop through the list to ping, DNS and AD
  foreach ($server in $list) {
      $result = Test-NetConnection -ComputerName $server -Port 3389 -InformationLevel Detailed
      $output.Hostname         = $server
      $output."RDP_Pass_?"     = $result.TcpTestSucceeded
      $output."DNS_Resolved_?" = $result.NameResolutionSucceeded
           
      #if DNS resolution works then get information on server
      if ($result.NameResolutionSucceeded -eq "True") {
          #Need to split the server name and the domain to find the server.
          try{
              $charSplit = $server.split('.', 2)
              $output.DName = Get-ADComputer -Identity $charSplit[0] -Properties DistinguishedName, Enabled  -Server $charSplit[1] | Select-Object -Expand DistinguishedName
          }
          catch{
             $output."DName" = "Check for StandAlone System"
          }
      }
      else {
          $output."DName" = "No AD Object"
      }
      [PSCustomObject]$output
  }
    
    
  $output | Export-Csv "C:\Temp\Acctlist_$((Get-Date).ToString('MM-dd-yyyy_HH-mm')).csv" -NoTypeInformation -Force
windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You're creating a PSCustomObject on line 34. That's good. But on line 14 you use the "ForEach (<expression)" to manage the loop. The problem with that is that that form of "ForEach" is interpreted as part of the PowerShell language and not as a cmdlet. The result is there's no pipelining and, because of that line 34's result is simply discarded.

Instead, use the ForEach-Object {<code block>} format which will be interpreted to use the ForEach-Object cmdlet -- which does enable the use of pipelining. You can eliminate the use of the $final array. Change references of $server to $<underbar>, and pipe the output of your line 34 into the Export-CSV cmdlet.

 # results template
 $output = [ordered]@{
     Hostname         = ""
     'RDP_Pass_?'     = ""
     'DNS_Resolved_?' = ""
     DName            = ""   
 }
            
 #Get the csv file
 # and loop through the list of server names to ping, DNS and AD
 Get-Content -Path C:\temp\server.txt |
     ForEach-Object {
         $result = Test-NetConnection -ComputerName $_ -Port 3389 -InformationLevel Detailed
         $output.Hostname = $_
         $output."RDP_Pass_?" = $result.TcpTestSucceeded
         $output."DNS_Resolved_?" = $result.NameResolutionSucceeded
                
         #if DNS resolution works then get information on server
         if ($result.NameResolutionSucceeded -eq "True") {
             #Need to split the server name and the domain to find the server.
             try {
                 $charSplit = $_.split('.', 2)
                 $output.DName = Get-ADComputer -Identity $charSplit[0] -Properties DistinguishedName, Enabled  -Server $charSplit[1] | 
                                     Select-Object -Expand DistinguishedName
             }
             catch {
                 $output."DName" = "Check for StandAlone System"
             }
         }
         else {
             $output."DName" = "No AD Object"
         }
         [PSCustomObject]$output
     } | Export-Csv "C:\Temp\Acctlist_$((Get-Date).ToString('MM-dd-yyyy_HH-mm')).csv" -NoTypeInformation -Force


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.