vincent1890
vincent1890

Reputation: 15

How to fix ‘value null Copy-Item Powershell' Error

I want to create a powershell installer/uninstaller for mdt but I end up with the following errors.

The code works correctly, it copies well the entire file architecture and folder in the destination but with errors at the end and I do not understand what exactly is happening and what are the problems

"Impossible d’appeler une méthode dans une expression Null."

"Cannot call a method in a Null expression."

Impossible d’appeler une méthode dans une expression Null. Au caractère C:\Users\Administrateur\Desktop\Notepad++\ScriptInstallNotepad++\Install_NotepadPlusPlus.ps1:24 : 3 + $dir = $item.DirectoryName.Replace($fromFolder,$toFolder) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation : (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Test-Path : Impossible de lier l'argument au paramètre « Path », car il a la valeur Null. Au caractère C:\Users\Administrateur\Desktop\Notepad++\ScriptInstallNotepad++\Install_NotepadPlusPlus.ps1:26 : 24 + if (!(test-path($dir))) + ~~~~~~ + CategoryInfo : InvalidData : (:) [Test-Path], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

My script powershell

# 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

Upvotes: 0

Views: 1063

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36277

So, as I mentioned in my comment, you're referencing a non-existent property when $item is a folder, and trying to call a method on that, so you're going to get errors. Really though, you're doing this the hard way. PowerShell will recursively copy things for you, there's no need to do it manually like you are. Instead of writing your own function just do this:

# Make sure the destination folder exists
If(!(Test-Path $DestinationLanguageNotepadPlusPlus)){New-Item $DestinationLanguageNotepadPlusPlus -ItemType Directory -Force|Out-null}
# Copy source folder contents to destination folder recursively
Copy-Item "$SourceLanguageNotepadPlusPlus\*" -dest "$DestinationLanguageNotepadPlusPlus" -recurse -force

Upvotes: 1

Related Questions