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.