【发布时间】:2013-09-18 07:09:52
【问题描述】:
我正在制作一个搜索文件的脚本。如果找到文件,它将在服务器上写入 txt 文件,其中包含计算机名称和文件所在的路径。
$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) 我们正在使用可以部署程序和启动 powershell 脚本的 3rd 方程序。
问题是虽然该程序以管理员用户身份启动脚本,但不允许 powershell 脚本搜索 C:\Users[some_other_user] 文件夹中的文件。
有没有办法强制搜索受限文件夹?
2) 第二个问题不是那么重要,但是否有可能以某种方式在 $filePath 中不仅包含 C:,还包含 D:?
======================================= 编辑: 好吧,我认为现在的问题更糟。它不仅搜索用户文件夹,而且根本搜索 C 盘......由于某种原因我没有注意到这一点。
所以问题不在于 powershell 脚本,而在于 3rd 方部署工具。在物理计算机上手动启动脚本时,它可以工作。
所以我最好想办法以其他方式启动此脚本,是否有其他方式可以为用户启动此脚本?
我从 cmd.exe 在本地机器上尝试过:
powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1
它只返回了 D: 和 I: 的搜索结果,但忽略了 C:
=======================================EDIT2: 我以普通用户复制粘贴脚本启动 Powershell,它可以工作,它在 C 盘上找到一个文件!
如果我以管理员复制粘贴脚本的身份启动 Powershell,则它不起作用,它在 C 盘上找不到文件。
【问题讨论】:
-
当然,为了让脚本更短,我会删除 Write-Host 并在稍后放置 -ne $Null。