0

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?

====================================== EDIT: Ok, I think the problem now is even worse. It doesn't search not only user folder but C disk at all... 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 it works.

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:

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

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

Phoneutria
  • 351
  • 4
  • 9
  • 17

1 Answers1

0

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?

By default only the the owner of a profile folder (usually the respective user) has access to the folder. To allow access for other users you need to take ownership of the folder as an administrator and then re-assign permissions. It's common practice to grant full control to SYSTEM, Administrators and the user the profile belongs to.

You can define a policy that changes the default behavior so that SYSTEM and Administrators are automatically granted access to newly created profiles.

Note that searching other users' profiles may have privacy implications, so make sure that it's actually legal for you to do this.

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

Get-ChildItem takes a string array, so you just need to change

$filePath = "c:"

into

$filePath = "c:", "d:"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328