none
How to Search for a Specific file on Remote Computers RRS feed

  • Question

  • This works on my local machine but not when I try to run on remote Machine

    $logFile = 'c:\scripts\file\test.log'
    ##$Filename1 = "$env:LOCALAPPDATA\Temp\filetest.pdf"
    ##$Filename2 = "$env:LOCALAPPDATA\Temp\filetest.exe"
    
    If (Test-Path $Filename1){
    Write-Host "Exist"
    }Else{
    Write-Host "Does Not Exist"
    }
    If (Test-Path $Filename2){
    Write-Host "Exist"
    }Else{
    Write-Host "Does Not Exist"
    }

    The Biggest problem is trying to figure out the path since it is in user profiles??


    • Edited by cfreeman21 Monday, March 2, 2015 8:18 PM code issue
    Monday, March 2, 2015 8:18 PM

Answers

  • Are you looking for a specific user or do you want to scan each user?


    If you are:

    $computer = "\\computername"
    $users = get-childitem $computer\c$\users
    
    foreach ($user in $users) { 
        if(test-path "$computer\c$\users\$user\AppData\Local\Temp\filetest.pdf") { write-host "File filetest.pdf Found in Profile $user" }
    }


    Entrepreneur, Strategic Technical Advisor, and Sr. Consulting Engineer - Strategic Services and Solutions Check out my book - Powershell 3.0 - WMI: http://amzn.to/1BnjOmo | Mastering PowerShell Coming in April 2015!


    • Edited by Brent Blawat Monday, March 2, 2015 9:48 PM Updated code
    • Proposed as answer by Brent Blawat Tuesday, March 3, 2015 4:12 PM
    • Marked as answer by cfreeman21 Tuesday, March 3, 2015 4:16 PM
    Monday, March 2, 2015 9:43 PM

All replies

  • If you are a full administrator then:

    #
    Test-Path \\PCNAME\C$\Users\<username>\APPDATA\LOCAL\TEMP\filetest.pdf
    #
    

    ¯\_(ツ)_/¯




    • Edited by jrv Monday, March 2, 2015 9:00 PM
    Monday, March 2, 2015 8:58 PM
  • Yes I am Full Administrator on all computers... But I want to do a ForEach on all computers are so doing like %username% will just look for my profile on those computers.
    Monday, March 2, 2015 9:08 PM
  • Then as an administrator of course you know that %balhblah% variables are only available when you are logged in.

    Start with this:

    Get-ChildItem \\computer\users |
        ForEach{
       }


    ¯\_(ツ)_/¯

    Monday, March 2, 2015 9:16 PM
  • Yes I am Full Administrator on all computers... But I want to do a ForEach on all computers are so doing like %username% will just look for my profile on those computers.
    Are you looking for a specific user or do you want to scan each user?

    Entrepreneur, Strategic Technical Advisor, and Sr. Consulting Engineer - Strategic Services and Solutions Check out my book - Powershell 3.0 - WMI: http://amzn.to/1BnjOmo | Mastering PowerShell Coming in April 2015!

    Monday, March 2, 2015 9:35 PM
  • Are you looking for a specific user or do you want to scan each user?


    If you are:

    $computer = "\\computername"
    $users = get-childitem $computer\c$\users
    
    foreach ($user in $users) { 
        if(test-path "$computer\c$\users\$user\AppData\Local\Temp\filetest.pdf") { write-host "File filetest.pdf Found in Profile $user" }
    }


    Entrepreneur, Strategic Technical Advisor, and Sr. Consulting Engineer - Strategic Services and Solutions Check out my book - Powershell 3.0 - WMI: http://amzn.to/1BnjOmo | Mastering PowerShell Coming in April 2015!


    • Edited by Brent Blawat Monday, March 2, 2015 9:48 PM Updated code
    • Proposed as answer by Brent Blawat Tuesday, March 3, 2015 4:12 PM
    • Marked as answer by cfreeman21 Tuesday, March 3, 2015 4:16 PM
    Monday, March 2, 2015 9:43 PM
  • This is working for 1 computer.. Having issues trying to run on multiple computers... Advise?
    Tuesday, March 3, 2015 8:29 PM
  • Now is when you realize that you need to learn PowerShell.,  The answer to your question is in the first two chapters of most books,

    Start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    "Asking incremental questions in a forum is a very inefficient way to learn scripting."  (Quote from a famous PosHer)


    ¯\_(ツ)_/¯

    Tuesday, March 3, 2015 8:33 PM