Get the list of server names which does not have file on remote servers

Welcome Forums General PowerShell Q&A Get the list of server names which does not have file on remote servers

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

 
Participant
4 months ago.

  • Author
    Posts
  • #102835

    Participant
    Points: 0
    Rank: Member

    I have a script, which will provide me the the server names, If file exists. I am stuck up with small issue. Looking for the output: If the file is present, ignore. If the file is not present, get the output with server name. Can someone please help on this? Below is my script:

    function check-remotefile {
     
        PROCESS {
     
                    $file = "\\$_\c$\Program Files (x86)\test\access\tomcatupgrade.txt"
     
                    if (test-path $file)
     
                    {
     
                    echo "$_ file tomcatupgrade.txt exists"
     
                    }
     
                }
     
    }
     
    Get-Content  C:\Users\venkatak\Desktop\Scripts\Tomcat_Check\Computers.txt | check-remotefile
    
    

    -Kalyan

  • #102836

    Participant
    Points: 9
    Rank: Member

    If you like your function to take pipeline input you will have to add a param block and tell it to do so. 😉 Start with reading the help for it:

    Get-Help about_Functions_Advanced_Parameters
  • #102848

    Participant
    Points: 0
    Rank: Member

    I think you just need an ELSE statement

    function check-remotefile {
     
        PROCESS {
     
                    $file = "\\$_\c$\Program Files (x86)\test\access\tomcatupgrade.txt"
     
                    if (test-path $file)
     
                    {
     
                    echo "$_ file tomcatupgrade.txt exists"
     
                    }
                    else 
                    {
                        Write-output "$_ file does NOT exist"'
                    }
     
                }
     
    }
    
    

    BTW: Echo is just an alias for Write-Output.

    This function is not typical, it *should* work for pipeline input. Take the advice of Olaf and use parameters, it will make it easier to read and debug in the future. Plus it will work better too.

    • #102857

      Participant
      Points: 0
      Rank: Member

      one line:

      "localhost","127.0.0.1"  | % {if(!(test-path "c:\temp\test.txt")){Write-Output "$_ File not found"}}
      

The topic ‘Get the list of server names which does not have file on remote servers’ is closed to new replies.