Getting server name from a list

Welcome Forums General PowerShell Q&A Getting server name from a list

This topic contains 10 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 6 days ago.

  • Author
    Posts
  • #125579

    Participant
    Points: 21
    Rank: Member

    Hi

    I am trying to get some details from a list of servers, and put the server name beside it.

    But the only server name being returned is the last one on the list.

    If anyone has any tips or hints about how to get the results for all servers that would be great

    Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt" |
    
    ForEach-Object{
    
    Get-PIBackupSummary -Connection (Connect-PIDataArchive -PIDataArchiveMachineName $_)
    
     } |
    
    Where-Object{ $_.StatusMessage -eq '[0] Success'} |
    
    Select-Object @{Name="Server";Expression={$Server}},status,statusmessage,type,backupend |
    
    Export-Csv "P:\Powershell\Friday Checks\Log_Errors.csv" -NoTypeInformation -Encoding UTF8
  • #125586

    Moderator
    Points: 171
    Team MemberHelping Hand
    Rank: Participant

    Take a look at the closing foreach "}" placement. You need to move this to the end of the Select, then pipe to Export-csv.

  • #125595

    Participant
    Points: 21
    Rank: Member

    Take a look at the closing foreach "}" placement. You need to move this to the end of the Select, then pipe to Export-csv.

    Hi Thanks for your reply , have made the change but still the same results, the last servername on the list is the only one returned for all results

    Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt" |
    
    ForEach-Object{
    
    Get-PIBackupSummary -Connection (Connect-PIDataArchive -PIDataArchiveMachineName $_)|
    
    Where-Object{ $_.StatusMessage -eq '[0] Success'} |
    
    Select-Object @{Name="Server";Expression={$Server}},status,statusmessage,type,backupend} |
    
    Export-Csv "P:\Powershell\Friday Checks\Log_Errors.csv" -NoTypeInformation -Encoding UTF8
  • #125597

    Moderator
    Points: 171
    Team MemberHelping Hand
    Rank: Participant

    Does removing the export-csv give you the desired results in the console?

  • #125600

    Participant
    Points: 21
    Rank: Member

    Does removing the export-csv give you the desired results in the console?

    Hi

    Unfortunately not,  results in console matches the output file

    sample

    Server Status StatusMessage Type BackupEnd
    ho045s 0 [0] Success Full 21/09/2018 17:45
    ho045s 0 [0] Success Incremental 22/09/2018 02:15
    ho045s 0 [0] Success Incremental 23/09/2018 02:15
  • #125604
    js

    Participant
    Points: 315
    Helping Hand
    Rank: Contributor

    What is $server? I think you mean $_.server?

  • #125636

    Moderator
    Points: 171
    Team MemberHelping Hand
    Rank: Participant

    Here is one way, tweek it for your use.
    This example, while trivial, shows how to create an object with data from an original CSV and adding data.
    Not sure what information you have in your original csv file.

    Import-Csv C:\TEMP\test.csv | foreach { 
        $computer = Get-ADComputer $_.server -Properties OperatingSystem
        [psCustomObject] @{
            NameFromFile = $_.server
            NoteFromFile = $_.Note
            OperatingSystem = $computer.operatingsystem
        }
    } | Export-Csv C:\TEMP\testResult.csv -NoTypeInformation
    
    
  • #125655

    Participant
    Points: 31
    Rank: Member

    I think you need to do this differently using a ForEach loop (The code below is untested)

    foreach ($computer in $(Get-Content "P:\Powershell\Friday Checks\Pi_servers.txt")) {
      Get-PIBackupSummary -Connection (Connect-PIDataArchive -PIDataArchiveMachineName $computer) |
      Where-Object{ $_.StatusMessage -eq '[0] Success'} |
      select @{name="ServerName";expression={$computer}},status,statusmessage,type,backupend }
    

    I have used this technique for other commands that do not have the server name as a property. You could also create a PSObject in the code as well., I am pretty sure that objects are necessary. So use some of the code above for the PSObjs.   Emphasis is mine in the code. Hope this helps or gets you closer to your objective. Good Luck

  • #125660

    Participant
    Points: 31
    Rank: Member

    Quickly put together a solution that uses the PSObjects.   This is a dumb example but it was tested.

     

    $(
        ForEach ($computer in $(gc C:\temp\srv.csv)) {
            $result = Test-Connection $computer -Count 1 
            New-Object psobject -Property @{
                ServerName = $computer
                IPAddress = ($result.IPV4Address).tostring()
            }
        }
    ) | Export-Csv .\test.csv -NoTypeInformation 
    

    Good luck

  • #125759

    Participant
    Points: 21
    Rank: Member

    have used this technique for other commands that do not have the server name as a property. You could also create a PSObject in the code as well., I am pretty sure that objects are necessary. So use some of the code above for the PSObjs. Emphasis is mine in the code. Hope this helps or gets you closer to your objective. Good

    Thank you so much for your help (and everyone else !) This works exactly-  now to try export it to csv!

  • #125775

    Participant
    Points: 31
    Rank: Member

    Pretty sure you will need to use a PSObject since the Export-CSV command expects objects as input. The foreach loop is going to produce text strings.

You must be logged in to reply to this topic.