none
Powershell script to find files in restricted folders as well

    Question

  • I'm making a script which would search for a file. If file is found it will write txt file on server with computer's name and path where file is located.

    $filePath = "c:"

    $fileName = "somefile"

    $computerName = Get-Content env:computername

    $srvPath = "\\server\share$\FindFileScript\$computerName.txt"

    #SEARCH FOR FILE ON C DISK

    $fileResult = (Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue

    | Where-Object { $_.Name -like "*$fileName*" } | Select-Object FullName | format-Table * -AutoSize)

    if ($fileResult -eq $Null)

    {

    Write-Host "File named $fileName on disk C was not found."

    }

    Else

    {

    Write-Host "File named $fileName on disk C was found."

    Out-File -FilePath $srvPath -InputObject $fileResult

    }



    1. We are using 3rd party program that can deploy programs and launch powershell script as well.

    The problem  is though that program launch script as Administrator user so powershell script is not allowed to search for files on C:\Users\[some_other_user] folder.

    Is there a way to force search on restricted folders as well?

    2. Second question is not so important but is it possible somehow to include in $filePath not only C: but D: as well?


    • Edited by Ph0neutr1a Wednesday, September 18, 2013 7:20 AM
    Wednesday, September 18, 2013 7:02 AM

Answers

  • Looks like I found what is the problem.

    Powershell starts in C:\Windows\System32

    And powershell.exe starts in C:\Windows\System32\WindowsPowershell\v1.0

    If I type C: it goes there.

    So I edited script and add just cd.. four times and it works now just fine.
    Thursday, September 19, 2013 11:15 AM

All replies

  • 1. turn off UAC

    2. $filepath = 'C:\','D:\'

    Wednesday, September 18, 2013 12:00 PM
  • 1. turn off UAC

    2. $filepath = 'C:\','D:\'


    1. I disabled UAC but it didn't find on user desktop a file when I was testing a powershell script.

    2. Thanks, this works!

    Wednesday, September 18, 2013 12:54 PM
  • For #1, the account that the script is running as will need permission to read (or at the very least list) the files. There's no way around this (that I'm aware of, at least). Are you able to adjust which account is being used?

    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    Wednesday, September 18, 2013 1:18 PM
  • Ok, I think the problem now is even worse. It doesn't search not only user folder but C disk at all... it search D: disk as well as mounted user disk from server, by some reason I didn't notice this.

    So problem is not with powershell script but with 3rd party deployment tool. When launching script manually on physical computer everything works fine.

    So I'd better find a way how to launch this script in other way, is there other way I could launch this script for users?

    I tryed on local machine from cmd.exe:

    powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1
    It returned search results on D: and I: only but ignored C:






    • Edited by Ph0neutr1a Wednesday, September 18, 2013 2:19 PM
    Wednesday, September 18, 2013 2:01 PM
  • I tryed on local machine from cmd.exe:

    powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1
    It returned search results on D: and I: only but ignored C:

    How about running CMD as administrator?

    http://technet.microsoft.com/en-us/magazine/ff629472.aspx

    Or just start powershell as administrator.

    Regards,

    Yan Li

    TechNet Subscriber Support

    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Cataleya Li
    TechNet Community Support



    Thursday, September 19, 2013 2:28 AM
    Moderator
  • I tried to launch cmd as administrator.

    I tried even

    Set-ExecutionPolicy unrestricted
    and then script.ps1. It didn't find a file on C disk.

    • Edited by Ph0neutr1a Thursday, September 19, 2013 6:59 AM
    Thursday, September 19, 2013 6:43 AM
  • Ok, one more weird thing.

    So I have a file on each disk - C, D, and I (user disk on server)

    Manually on computer -

    I start Powershell as normal user copy-paste the script and it works, it finds a file on every disk!

    If I start Powershell as administrator copy-paste script then it doesn't work, it doesn't find a file on C and I disk, it found a file only on D.

    I guess something is wrong with permissions then, I will upload screenshots of C and D disks.
    Sorry they are in German language -

    http://oi44.tinypic.com/5konqd.jpg

    http://oi39.tinypic.com/2s17no7.jpg

    • Edited by Ph0neutr1a Thursday, September 19, 2013 7:33 AM
    Thursday, September 19, 2013 7:02 AM
  • Looks like I found what is the problem.

    Powershell starts in C:\Windows\System32

    And powershell.exe starts in C:\Windows\System32\WindowsPowershell\v1.0

    If I type C: it goes there.

    So I edited script and add just cd.. four times and it works now just fine.
    Thursday, September 19, 2013 11:15 AM
  • As a helpful note in order to get back to the root of the drive quickly, just type:

    cd\

    Good luck!

    Thursday, September 19, 2013 11:56 AM