如何修复“值null复制项Powershell”错误

时间:2019-08-08 23:30:39

标签: windows powershell null powershell-5.0 copy-item

我想为mdt创建一个Powershell安装程序/卸载程序,但最终出现以下错误。

代码可以正常工作,可以很好地复制目标中的整个文件体系结构和文件夹,但最终会出现错误,我不知道到底在发生什么,还有什么问题

  

“可能无法表达d'appeler和méthodedans”。

     

“无法在Null表达式中调用方法。”

     

表达式不可能为空。       辅助C:\ Users \ Administrateur \ Desktop \ Notepad ++ \ ScriptInstallNotepad ++ \ Install_NotepadPlusPlus.ps1:24:3       + $ dir = $ item.DirectoryName.Replace($ fromFolder,$ toFolder)       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~           + CategoryInfo:InvalidOperation:(:) [],RuntimeException           + FullyQualifiedErrorId:InvokeMethodOnNull

     

Test-Path:不可能的参数«Path»,不能为空。       辅助C:\ Users \ Administrateur \ Desktop \ Notepad ++ \ ScriptInstallNotepad ++ \ Install_NotepadPlusPlus.ps1:26:24       +如果(!(test-path($ dir)))       + ~~~~~~           + CategoryInfo:InvalidData:(:) [Test-Path],ParameterBindingValidationException           + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

我的脚本功能强大

# Script install App MDT
# ----------- Modifier variable apres cette ligne -----------
# ------------- Modify variable after this line -------------

$NameApp = "Notepad++"
$Installer32 = "npp.7.7.1.Installer.exe"
$Installer64 = "npp.7.7.1.Installer.x64.exe"
$arguments = "/S"
$uninstaller32or64 = "Notepad++\uninstall.exe"
$argumentsUninstall = "/S"

# --------------- Ne rien modifier apres cette ligne ---------------
# ------------- Do not modify anything after this line -------------

$SourceLanguageNotepadPlusPlus = "$(Get-Location)\AppDadaNotepad++Hidden\Notepad++"
$SourcePluginNotepadPlusPlus = "$(Get-Location)\ComparePlugin"
$DestinationLanguageNotepadPlusPlus = "C:\Users\Default\AppData\Roaming\Notepad++"
$DestinationPluginNotepadPlusPlus = "C:\Program Files\Notepad++\plugins\ComparePlugin"

function CopyFilesToFolder ($fromFolder, $toFolder) {
    $childItems = get-childitem $fromFolder -recurse
    foreach ($item in $childItems)
    {
        $dir = $item.DirectoryName.Replace($fromFolder,$toFolder)
        $target = $item.FullName.Replace($fromFolder,$toFolder)
        if (!(test-path($dir)))
        {
            mkdir $dir
        }
        if (!(test-path($target)))
        {
            copy-item -path $item.FullName -destination $target -recurse -force
        }
    }
}

# Uninstall
Write-Host "Uninstall $NameApp" -ForegroundColor Cyan
If ((Test-Path "${env:ProgramFiles(x86)}\Notepad++\uninstall.exe" -PathType Leaf) -or (Test-Path "${Env:ProgramFiles}\Notepad++\uninstall.exe" -PathType Leaf))
{
    If (Test-Path "${env:ProgramFiles(x86)}\$uninstaller32or64" -PathType Leaf)
    {
        Write-Host "TEST Desinstallation $NameApp ProgramFilesX86" -ForegroundColor Magenta
        $executableSupprFinal = "${env:ProgramFiles(x86)}\$uninstaller32or64"
        start-process $executableSupprFinal $argumentsUninstall -PassThru -Verb RunAs -Wait #-NoNewWindow
        Write-Host "Desinstallation $NameApp reussi" -ForegroundColor Yellow
    }
    elseif (Test-Path "${Env:ProgramFiles}\$uninstaller32or64" -PathType Leaf)
    {
        Write-Host "TEST Desinstallation $NameApp ProgramFiles" -ForegroundColor Magenta
        $executableSupprFinal = "${env:ProgramFiles}\$uninstaller32or64"
        start-process $executableSupprFinal $argumentsUninstall -PassThru -Verb RunAs -Wait #-NoNewWindow
        Write-Host "Desinstallation $NameApp reussi" -ForegroundColor Yellow
    }
    else
    {
        Write-Host "Desinstaller $NameApp introuvable" -ForegroundColor Red
    }
}
else
{
    Write-Host "$NameApp NON presente" -ForegroundColor Green
}


# Install
Write-Host "Installation $NameApp" -ForegroundColor Green
If (Test-Path "${env:ProgramFiles(x86)}")
{
    $Installer = $Installer64
    $InstallerFinal = "$(Get-Location)\$Installer"
    start-process $InstallerFinal $arguments -PassThru -Verb RunAs -Wait #-NoNewWindow
    #Copy Item from Deployroot
    Write-Host "Copie auxiliere $NameApp" -ForegroundColor Green
    CopyFilesToFolder "$SourceLanguageNotepadPlusPlus" "$DestinationLanguageNotepadPlusPlus"
    CopyFilesToFolder "$SourcePluginNotepadPlusPlus" "$DestinationPluginNotepadPlusPlus"
}
Else 
{
    $Installer = $Installer32
    $InstallerFinal = "$(Get-Location)\$Installer"
    start-process $InstallerFinal $arguments -PassThru -Verb RunAs -Wait #-NoNewWindow
    #Copy Item from Deployroot
    Write-Host "Copie auxiliere $NameApp" -ForegroundColor Green
    CopyFilesToFolder "$SourceLanguageNotepadPlusPlus" "$DestinationLanguageNotepadPlusPlus"
    CopyFilesToFolder "$SourcePluginNotepadPlusPlus" "$DestinationPluginNotepadPlusPlus"
}

Write-Host "Fin install $NameApp" -ForegroundColor Green

1 个答案:

答案 0 :(得分:1)

因此,正如我在评论中提到的那样,当t2[t2[pks].apply(tuple,1).isin(t1[pks].apply(tuple,1))] 是文件夹时,您引用的是不存在的属性,并尝试在该属性上调用方法,因此您将得到错误。确实,您确实很难做到这一点。 PowerShell将为您递归复制内容,无需像您一样手动进行操作。不用编写自己的函数,只需执行以下操作:

$item