Powershell文件长度异常问题
我一直在尝试运行这个脚本,它捕获共享目录中的所有文件/文件夹,并将其传递回splunk。然而,脚本给出了PathTooLongException,尽管我能找到的最长路径是197个字符 我曾尝试将一个共享映射为C驱动器根目录下一个名为z的文件夹,但根本没有帮助。有人能解释一下如何绕过这个问题,或者是什么导致了这些错误吗Powershell文件长度异常问题,powershell,pathtoolongexception,Powershell,Pathtoolongexception,我一直在尝试运行这个脚本,它捕获共享目录中的所有文件/文件夹,并将其传递回splunk。然而,脚本给出了PathTooLongException,尽管我能找到的最长路径是197个字符 我曾尝试将一个共享映射为C驱动器根目录下一个名为z的文件夹,但根本没有帮助。有人能解释一下如何绕过这个问题,或者是什么导致了这些错误吗 param ( [string]$directory = "C:\Windows\Logs" ) $errorCount = 0 $OutputList = @() $dire
param (
[string]$directory = "C:\Windows\Logs"
)
$errorCount = 0
$OutputList = @()
$directoryLink = 'c:\z'
#set up symbolic link
cmd /c mklink /d $directoryLink $directory
try
{
$colItems = (Get-ChildItem -Path $directoryLink -Recurse | Sort-Object)
}
catch
{
$errorCount += 1
}
foreach ($i in $colItems)
{
try
{
$subFolderItems = (Get-Item $i.FullName | Measure-Object -property
length -sum -ErrorAction SilentlyContinue)
$acl=$i.GetAccessControl()
$SizeValue= [Math]::Round(($subFolderItems.sum / 1MB),3)
$SplunkFileList = New-Object PSObject
$SplunkFileList | Add-Member -type NoteProperty -name Filename -Value $i.FullName
$SplunkFileList | Add-Member -type NoteProperty -name SizeMb -Value $SizeValue
$SplunkFileList | Add-Member -type NoteProperty -name LastAccess -Value $i.LastAccessTime
$SplunkFileList | Add-Member -type NoteProperty -name Owner -Value $acl.Owner
if ($i.PSIsContainer)
{
$SplunkFileList | Add-Member -type NoteProperty -name Type -Value "D"
}
else
{
$SplunkFileList | Add-Member -type NoteProperty -name Type -Value "F"
}
$OutputList += $SplunkFileList
}
catch [System.IO.IOExeception]
{
Write-Host 'An Exception was caught.'
Write-Host "Exception : $($_.Exception.GetType().FullName)"
$errorCount += 1
}
}
$OutputList | Select-Object Filename,SizeMb,LastAccess,Owner,Type | Format-
List
如果您使用的是现代版本的Powershell(我想是v5.1+版本),那么可以使用unicode版本的Windows API访问长度超过260个字符的路径 为此,请在路径前面加上
\\?\
前缀,例如:
Get-ChildItem -LiteralPath '\\?\C:\Windows\Logs' -Recurse
注意:LiteralPath
而不是Path
如果要使用UNC路径(
\\server\C$\folder
),语法略有不同:
Get-ChildItem -LiteralPath '\\?\UNC\server\C$\folder' -Recurse
非常感谢,不幸的是,splunk本机使用的是powershell 2.0。我猜没有办法解决这个问题了?这是一个古老的PS版本,只出现在2008/2008R2服务器上。如果您正在使用这些操作系统,您需要更新的不仅仅是Powershell!唯一的另一种方法是类似于库的方法,但我没有像现代PS版本那样使用它。:Robocopy用于递归搜索文件夹结构,以查找具有超过一定数量字符的文件或文件夹名称。函数返回一个具有三个属性的对象:FullPath、Type和FullPath。