如何在返回名称与驱动器相关的文件时查询临时 PS 驱动器? [英] How can I query a temporary PS-Drive while returning files with a name relative to the drive?

查看:67
本文介绍了如何在返回名称与驱动器相关的文件时查询临时 PS 驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,我将在其中搜索文件服务器的非继承权限.结果,我遇到了文件名 260 个字符的限制.我看到的一个建议,我认为会有所帮助,有几次是创建一些非持久性的 PS 驱动器,深度为几个级别并查询它们.

I am trying to create a script where I will be searching the file servers for non inherited permissions. I have run into the 260 character limit for file names as a result. A suggestion I saw, that I thought would help, a couple of times was to create some non persistent PS Drives a couple of levels deep and query those.

问题是当我对新的 PS 驱动器使用 Get-ChildItem 时,它返回带有完整网络路径的对象,使用我分配的名称.

Problem is when I use Get-ChildItem against the new PS Drives it is returning object with the full network path and not using the name I assigned it.

# Cycle the folders
Get-ChildItem $rootPath -Directory | select -first 1 | ForEach-Object{
    $target = $_

    # Create a PS Drive for each sub directory and get all the folders
    [void](New-PSDrive -Name $target.Name -PSProvider FileSystem $target.FullName)

    # Get the file objects. 
    Get-ChildItem "$($target.Name):\" -Recurse
}

我确信,如果我使用驱动器号创建了一些合适的持久网络驱动器,我就不会遇到这个问题.

I am sure that if I created some proper persistent network drives with a drive letter I would not have this issue.

希望我没有错过,但 Technet for New-PSDrive 不是 100% 清楚这种情况.

Hopefully I just didn't miss it but Technet for New-PSDrive was not 100% clear about this scenario.

我正在寻找一种方法来制作 ps-drive 并引用那里的文件夹,同时返回相对于新驱动器名称的路径.考虑我制作的 psdrive (G:) 的输出,然后是我映射的网络驱动器之一 (M:).

I am looking for a way to make ps-drive and reference there folders while returning paths relative to the new drive name. Consider the output from a psdrive I made (G:) then one of my mapped network drives (M:).

PS M:\> Get-ChildItem G:\

    Directory: \\server01\COMMON\Folder

Mode                LastWriteTime     Length Name                                                                                                            
----                -------------     ------ ----                                                                                                            
d----         6/18/2011   8:14 AM            Folder 1                                                                                                          
d----         6/18/2011   8:14 AM            Folder 2 

PS M:\> Get-ChildItem M:\

    Directory: M:\

Mode                LastWriteTime     Length Name                                                                                                            
----                -------------     ------ ----                                                                                                            
d----          5/8/2015  11:00 AM            Backup                                                                                                          
d----          5/8/2015  11:00 AM            covers                                                                                                          
d----          5/8/2015  11:00 AM            drop                                                                                                            
d----          5/8/2015  11:00 AM            Expense         

<小时>

我知道针对我的具体情况存在多种解决方法,但我想了解我使用 New-PSDrive 显示的行为.

推荐答案

看起来您混淆了两种不同的东西:PowerShell 路径和提供程序路径.PowerShell 路径在 PowerShell 之外不可见.

Looks like you are mixing up two different things: PowerShell path and Provider path. PowerShell paths are not visible outside of PowerShell.

New-PSDrive X FileSystem C:\Windows
(Get-Item X:\System32\notepad.exe).get_Length() #OK
([IO.FileInfo]'X:\System32\notepad.exe').get_Length() #Error

但是 Get-Item X:\System32\notepad.exe 设法创建了一个 FileInfo 对象,它代表某个文件.那么,生成的 FileInfo 对象代表什么文件?

But Get-Item X:\System32\notepad.exe managed to create a FileInfo object, which represents some file. So, what file is represented by the resulting FileInfo object?

(Get-Item X:\System32\notepad.exe).FullName
# C:\Windows\System32\notepad.exe

由于 FileInfo 对象对 PowerShell 驱动器 X: 一无所知,因此它必须存储一个路径,该路径在内部使用它可以理解的文件系统 API.您可以使用 Convert-Path cmdlet 将 PowerShell 路径转换为提供程序路径:

Since the FileInfo object knows nothing about PowerShell drive X:, it has to store a path, which internally uses the file system API which it can understand. You can use Convert-Path cmdlet to convert PowerShell path to Provider path:

Convert-Path X:\System32\notepad.exe
# C:\Windows\System32\notepad.exe

创建指向某个网络路径的 PowerShell 驱动器时也会发生同样的情况:

Same happens when you create the PowerShell drive, which point to some network path:

New-PSDrive Y FileSystem \\Computer\Share
Get-ChildItem Y:\

返回的 FileInfoDirectoryInfo 对象对 Y: 一无所知,因此它们不能具有相对于该 PowerShell 驱动器的路径.内部使用的文件系统 API 不会理解它们.

Returned FileInfo and DirectoryInfo objects know nothing about Y:, so they can not have paths relative to that PowerShell drive. Internally used file system API will not understand them.

当您使用 -Persist 选项时,情况会发生变化.在这种情况下,将创建真正的映射驱动器,这可以被 PowerShell 之外的文件系统 API 理解.

Things changes when you use the -Persist option. In that case real mapped drives will be created, which can be understood by file system API outside of PowerShell.

New-PSDrive Z FileSystem \\Computer\Share -Persist|Format-Table *Root
# Root        : Z:\
# DisplayRoot : \\Computer\Share

如您所见,Root 将不是您在 New-PSDrive cmdlet 中要求的 \\Computer\Share,而是 <代码>Z:\.由于 Z: 在这种情况下是一个真正的驱动器,FileInfoDirectoryInfo 对象由 Get-ItemGet-ChildItem cmdlet 可以有相对于它的路径.

As you can see, the Root will be not \\Computer\Share as you ask in New-PSDrive cmdlet, but Z:\. Since Z: is a real drive in this case, FileInfo and DirectoryInfo objects returned by Get-Item or Get-ChildItem cmdlet can have paths relative to it.

这篇关于如何在返回名称与驱动器相关的文件时查询临时 PS 驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆