首页 > 解决方案 > 如何使用不同的提升凭据在 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

谢谢

标签: powershell

解决方案


我想出了一个适合我的解决方案/解决方法。

我将以下脚本放在一个文件中。我将其改回使用 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}"

推荐阅读