powershell - 如何使用不同的提升凭据在 UNC 路径上运行 Get-ChildItems
问题描述
我有一个脚本(如下),可以检查用户在目录中保存了多少数据 - 此示例使用他们的漫游配置文件,但它应该适用于任何目录。该脚本最初在 Get-ChildItems 命令中直接使用了 UNC 路径。当使用管理员帐户运行脚本时,这工作正常(如预期的那样)。这样我就不必使用管理员帐户来运行我在 New-PSDrive 代码中添加的脚本,这样我就可以传递管理员凭据。如果我以管理员帐户运行此代码,它再次运行良好。此代码如下:
$username = "test_username"
$domian_user = "DOMAINNAME\${username}"
$directory = "\\server_name\profiles$\${username}.V6\"
# map a network drive "
Remove-PSDrive -Name temp -ErrorAction SilentlyContinue
New-PSDrive -Name temp -PSProvider FileSystem -Root $directory | Out-Null
if (-Not (Test-Path -Path "temp:\")) {
Write-Host "ERROR: Directory not found - ""temp:\""" -ForegroundColor Red
} else {
# get a list of all files in the $directory folder structure. select the full "file path, owner, file size
$file_list = Get-ChildItem -Path "temp:\" -Recurse -Force -File | Select-Object Fullname, @{Name='Owner'; Expression={ $_.GetAccessControl().Owner }}, @{Name="Bytes"; Expression={ "{0:N0}" -f $_.Length }}, LastAccessTime, LastWriteTime, CreationTime
$file_list = $file_list | Where-Object {$_.Owner -eq $domian_user}
$total_size = ($file_list | Measure-Object -Sum Bytes).Sum
# update our output ojbect
$size_MB = "{0:n3}" -f ($total_size / 1MB)
Write-Host $directory
Write-Host " MB:" $size_MB
}
# removed the mapped drive
Remove-PSDrive -Name temp -ErrorAction SilentlyContinue
如果我以管理员身份运行它,则此方法有效,因此下一步是将凭据与 New-PSDrive 一起使用。我对使用凭据所做的更改如下:
...
# New-PSDrive -Name temp -PSProvider FileSystem -Root $directory | Out-Null
$adm_credentials = Get-Credential -Credential "DOMAINNAME\adm_username"
New-PSDrive -Name temp -PSProvider FileSystem -Root $directory -Credential $adm_credentials | Out-Null
...
但这给出了一个错误:
New-PSDrive : The network path was not found
At D:\scripts\test.ps1:13 char:1
+ New-PSDrive -Name temp -PSProvider FileSystem -Root $directory -Crede ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (temp:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
我认为这与我的用户无法访问我尝试映射的路径有关(这就是为什么我首先使用带有凭据的 New-PSDrive)。
如何使用提升的凭据在 UNC 路径上运行 Get-ChildItems?
我正在使用 powershell 版本:5.1.17763.1007
谢谢
解决方案
我想出了一个适合我的解决方案/解决方法。
我将以下脚本放在一个文件中。我将其改回使用 UNC 路径并添加了一些参数,以便我可以传入值:
param (
[Parameter(Mandatory=$true)][String]$Username,
[String]$Path = ""
)
$domian_user = "DOMAINNAME\${Username}"
if ($Path -eq "") {
$Path = "\\server_name\profiles$\${Username}.V6\"
}
if (-Not (Test-Path -Path $Path)) {
Write-Host ""
Write-Host "ERROR: Directory not found" -ForegroundColor Red
} else {
# get a list of all files if the $Path folder structure
# and select the full file name, owner, size
$file_list = Get-ChildItem $Path -Recurse -Force -File | Select-Object Fullname, @{Name='Owner'; Expression={ $_.GetAccessControl().Owner }}, @{Name="Bytes"; Expression={ "{0:N0}" -f $_.Length }}, LastAccessTime, LastWriteTime, CreationTime
$file_list = $file_list | Where-Object {$_.Owner -eq $domian_user}
$total_size = ($file_list | Measure-Object -Sum Bytes).Sum
$size_MB = "{0:n3}" -f ($total_size / 1MB)
Write-Host $Path
Write-Host " MB: ${size_MB}"
}
# keep the window open
Read-Host "Press [Enter] to close this window"
我更改了原始脚本以使用以下代码。这将使用管理员凭据打开一个新的 powershell 窗口/进程并在那里运行命令。我无法从脚本中取回数据,但我可以使用它。
$adm_credentials = Get-Credential -Credential "DOMAINNAME\adm_username"
$username = "test_username"
Start-Process powershell.exe -Credential $adm_credentials -ArgumentList "-file D:\scripts\UserDataAllocation2.ps1", "-user ${username}"
推荐阅读
- perl - 从排序块返回给出“在排序中使用未初始化的值”警告,但只有显式返回
- r - map() 和 sapply() 函数之间的主要区别是什么?
- python - 提高循环过滤重复链接的效率
- python - ImportError:导入 _ssl 时 DLL 加载失败:%1 不是有效的 Win32 应用程序
- firebase - Firebase 重复可能是因为离线行为
- python - 在书面记录中对演讲者对话进行分组
- html - 垂直对齐 CSS 网格项
- java - serialVersionUID 不匹配时如何读取序列化数据?
- ruby-on-rails - Ruby on Rail:如何使用 _destroy 作为属性在保存时销毁对象(未嵌套)?
- ios - iOS 上的自定义键盘