1

我有以下 powershell 脚本,它循环浏览文件列表并重命名它们。我想在下面的异常中加入一些错误检查和日志记录:

使用“1”参数调用“GetFile”的异常:“没有名为“/cmms”的 Web。” 在 C:\eam\Exportjbh.ps1:18 char:22 + $file = $web.GetFile <<<< ($FullPath) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

您不能在空值表达式上调用方法。在 C:\eam\Exportjbh.ps1:19 char:35 + $binary = $file.OpenBinary <<<< () + CategoryInfo : InvalidOperation: (OpenBinary:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

为“Write”和参数计数找到多个模棱两可的重载:“1”。在 C:\eam\Exportjbh.ps1:27 char:22 + $writer.write <<<< ($binary) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$Url = "https://....."
$UrlSub = "docs"
$FullPath = $Url + $UrlSub
$destinationFolder = "c:\022713\"
$sourceCsv = "c:\filename.CSV"

$Site = New-Object -TypeName Microsoft.SharePoint.SPSite $Url 
$web =  $Site.OpenWeb($UrlSub)
$fileObjects = Import-CSV $sourceCsv 

ForEach ($fileObject in $fileObjects) 
{
    $fileUrl = $fileObject.DOC_FILENAME.replace($Url,"")
    $file = $web.GetFile($FullPath)
        $binary = $file.OpenBinary()

    $dateTimeStamp = Get-Date -format s
    $newFileName = $fileObject.DocumentType + "_" + $fileObject.SAPObjectNumber + "_" +

$dateTimeStamp.replace(":","").replace("-","") $extension = [System.IO.Path]::GetExtension($file.Name)

    $stream = New-Object System.IO.FileStream(($destinationfolder + $newFileName + $extension), [System.IO.FileMode]::Create)
    $writer = New-Object System.IO.BinaryWriter($stream)
    $writer.write($binary)
    $writer.Close() } $web.Dispose()
4

0 回答 0