如何在返回名称与驱动器相关的文件时查询临时 PS-驱动器?
How can I query a temporary PS-Drive while returning files with a name relative to the drive?
我正在尝试创建一个脚本,我将在其中搜索文件服务器以获取非继承权限。结果,我将 运行 设置为文件名的 260 个字符限制。我看到了一个我认为会有所帮助的建议,有几次是创建一些非持久性 PS 深入驱动几个级别并查询它们。
问题是当我对新的 PS 驱动器使用 Get-ChildItem
时,它返回的对象带有完整的网络路径,而 没有 使用我分配的名称它。
# 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
}
我敢肯定,如果我创建了一些带有驱动器盘符的适当的永久性网络驱动器,我就不会遇到这个问题。
希望我没有错过它,但 Technet for New-PSDrive 并不是 100% 清楚这种情况。
我正在寻找一种制作 ps 驱动器并在其中引用文件夹的方法,同时返回相对于新驱动器名称的路径。考虑我创建的 ps 驱动器 (G:) 的输出,然后是我映射的网络驱动器之一 (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
中表现出的行为。
还没有测试过,但是如果你不反对使用 'subst' 这样的东西可能对你有用
function Get-FreeDriveLetter {
$drives = [io.driveinfo]::getdrives() | % {$_.name[0]}
$alpha = 65..90 | % { [char]$_ }
$avail = diff $drives $alpha | select -ExpandProperty inputobject
$drive = $avail[0] + ':'
$drive
}
$file = gi 'C:\temp\file.txt'
$fullname = $file.FullName
if ($fullname.length -gt 240) {
$drive = Get-FreeDriveLetter
$path = Split-Path $fullname
subst $drive $path
$subst = $true
rv path
$fullname = Join-Path $drive $(Split-Path $fullname -Leaf)
}
$fullname
看起来您混淆了两个不同的东西:PowerShell 路径和提供程序路径。 PowerShell 路径在 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
对象代表什么文件?
(Get-Item X:\System32\notepad.exe).FullName
# C:\Windows\System32\notepad.exe
由于 FileInfo
对象对 PowerShell 驱动器 X:
一无所知,它必须存储一个路径,该路径在内部使用它可以理解的文件系统 API。您可以使用 Convert-Path
cmdlet 将 PowerShell 路径转换为提供程序路径:
Convert-Path X:\System32\notepad.exe
# C:\Windows\System32\notepad.exe
创建指向某个网络路径的 PowerShell 驱动器时也会发生同样的情况:
New-PSDrive Y FileSystem \Computer\Share
Get-ChildItem Y:\
返回的 FileInfo
和 DirectoryInfo
对象对 Y:
一无所知,因此它们不能具有相对于该 PowerShell 驱动器的路径。内部使用的文件系统 API 不会理解它们。
当您使用 -Persist
选项时,情况会发生变化。在这种情况下,将创建真正的映射驱动器,PowerShell 之外的文件系统 API 可以理解它。
New-PSDrive Z FileSystem \Computer\Share -Persist|Format-Table *Root
# Root : Z:\
# DisplayRoot : \Computer\Share
如您所见,Root
不是您在 New-PSDrive
cmdlet 中要求的 \Computer\Share
,而是 Z:\
。由于 Z:
在这种情况下是一个真正的驱动器,因此 Get-Item
或 Get-ChildItem
cmdlet 返回的 FileInfo
和 DirectoryInfo
对象可以具有相对于它的路径。
PS C:\Users\kamlesh> $cred = Get-Credential
New-PSDrive -name "L" -PSProvider FileSystem -Root "\192.105.11.58\d$" -Credential $cred -Persist
我正在尝试创建一个脚本,我将在其中搜索文件服务器以获取非继承权限。结果,我将 运行 设置为文件名的 260 个字符限制。我看到了一个我认为会有所帮助的建议,有几次是创建一些非持久性 PS 深入驱动几个级别并查询它们。
问题是当我对新的 PS 驱动器使用 Get-ChildItem
时,它返回的对象带有完整的网络路径,而 没有 使用我分配的名称它。
# 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
}
我敢肯定,如果我创建了一些带有驱动器盘符的适当的永久性网络驱动器,我就不会遇到这个问题。
希望我没有错过它,但 Technet for New-PSDrive 并不是 100% 清楚这种情况。
我正在寻找一种制作 ps 驱动器并在其中引用文件夹的方法,同时返回相对于新驱动器名称的路径。考虑我创建的 ps 驱动器 (G:) 的输出,然后是我映射的网络驱动器之一 (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
中表现出的行为。
还没有测试过,但是如果你不反对使用 'subst' 这样的东西可能对你有用
function Get-FreeDriveLetter {
$drives = [io.driveinfo]::getdrives() | % {$_.name[0]}
$alpha = 65..90 | % { [char]$_ }
$avail = diff $drives $alpha | select -ExpandProperty inputobject
$drive = $avail[0] + ':'
$drive
}
$file = gi 'C:\temp\file.txt'
$fullname = $file.FullName
if ($fullname.length -gt 240) {
$drive = Get-FreeDriveLetter
$path = Split-Path $fullname
subst $drive $path
$subst = $true
rv path
$fullname = Join-Path $drive $(Split-Path $fullname -Leaf)
}
$fullname
看起来您混淆了两个不同的东西:PowerShell 路径和提供程序路径。 PowerShell 路径在 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
对象代表什么文件?
(Get-Item X:\System32\notepad.exe).FullName
# C:\Windows\System32\notepad.exe
由于 FileInfo
对象对 PowerShell 驱动器 X:
一无所知,它必须存储一个路径,该路径在内部使用它可以理解的文件系统 API。您可以使用 Convert-Path
cmdlet 将 PowerShell 路径转换为提供程序路径:
Convert-Path X:\System32\notepad.exe
# C:\Windows\System32\notepad.exe
创建指向某个网络路径的 PowerShell 驱动器时也会发生同样的情况:
New-PSDrive Y FileSystem \Computer\Share
Get-ChildItem Y:\
返回的 FileInfo
和 DirectoryInfo
对象对 Y:
一无所知,因此它们不能具有相对于该 PowerShell 驱动器的路径。内部使用的文件系统 API 不会理解它们。
当您使用 -Persist
选项时,情况会发生变化。在这种情况下,将创建真正的映射驱动器,PowerShell 之外的文件系统 API 可以理解它。
New-PSDrive Z FileSystem \Computer\Share -Persist|Format-Table *Root
# Root : Z:\
# DisplayRoot : \Computer\Share
如您所见,Root
不是您在 New-PSDrive
cmdlet 中要求的 \Computer\Share
,而是 Z:\
。由于 Z:
在这种情况下是一个真正的驱动器,因此 Get-Item
或 Get-ChildItem
cmdlet 返回的 FileInfo
和 DirectoryInfo
对象可以具有相对于它的路径。
PS C:\Users\kamlesh> $cred = Get-Credential New-PSDrive -name "L" -PSProvider FileSystem -Root "\192.105.11.58\d$" -Credential $cred -Persist