Partage
  • Partager sur Facebook
  • Partager sur Twitter

Erreur Copy-Item PowerShell

Erreur Copy-Item PowerShell

Sujet résolu
8 août 2019 à 1:59:45

Bonjour

Lors de l’exécution de ce script que je viens de créer j'ai des messages erreurs que je ne comprends pas, la copie de l'ensemble des les fichiers et de arborescence est bien faite.

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.TestPathCom
   mand






Script :

# 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
		# Move-Item -path "${env:ProgramFiles(x86)}\$DirUninstaller\NppShell_06.dll" -destination "$env:temp\$DirUninstaller\NppShell_06.dll"
		# Remove-Item -Path "${env:ProgramFiles(x86)}\$DirUninstaller" -Recurse -Force
		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
		# Move-Item -path "${env:ProgramFiles}\$DirUninstaller\NppShell_06.dll" -destination "$env:temp\$DirUninstaller\NppShell_06.dll"
		# Remove-Item -Path "${env:ProgramFiles}\$DirUninstaller" -Recurse -Force
		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"
	#Copy-Item -Path $SourceLanguageNotepadPlusPlus -Destination $DestinationLanguageNotepadPlusPlus -Recurse -Force 
	#Copy-Item -Path $SourcePluginNotepadPlusPlus -Destination $DestinationPluginNotepadPlusPlus -Recurse -Force 
}
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"
	#Copy-Item -Path $SourceLanguageNotepadPlusPlus -Destination $DestinationLanguageNotepadPlusPlus -Force -Recurse
	#Copy-Item -Path $SourcePluginNotepadPlusPlus -Destination $DestinationPluginNotepadPlusPlus -Force -Recurse
}

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




Merci de votre aide.

-
Edité par Vincent1890 9 août 2019 à 1:04:55

  • Partager sur Facebook
  • Partager sur Twitter
9 août 2019 à 16:34:56

N'ayant pas de réponse ici j'ai posté le sujet sur un autres forum et j'ai m'a réponse pour info je me compliquais la vie voici le code de m'a fonction modifier et du coup qui fonctionne :

https://stackoverflow.com/questions/57421841/how-to-fix-value-null-copy-item-powershell-error

function CopyFilesToFolder ($fromFolder, $toFolder) {
    # Make sure the destination folder exists
	If(!(Test-Path $toFolder)){New-Item $toFolder -ItemType Directory -Force|Out-null}
	# Copy source folder contents to destination folder recursively
	Copy-Item "$fromFolder\*" -Destination "$toFolder" -recurse -force
}



  • Partager sur Facebook
  • Partager sur Twitter