如何修复“value null Copy-Item Powershell”错误
How to fix ‘value null Copy-Item Powershell' Error
我想为 mdt 创建一个 powershell installer/uninstaller,但我最终遇到了以下错误。
代码工作正常,它很好地复制了目标中的整个文件架构和文件夹,但最后出现错误,我不明白到底发生了什么以及有什么问题
"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
我的脚本 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
所以,正如我在评论中提到的,当 $item
是一个文件夹时,你引用了一个不存在的 属性,并试图调用它的方法,所以你会出错。不过,实际上,您这样做很困难。 PowerShell 将递归地为您复制内容,无需像您一样手动执行。与其编写自己的函数,不如这样做:
# 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
我想为 mdt 创建一个 powershell installer/uninstaller,但我最终遇到了以下错误。
代码工作正常,它很好地复制了目标中的整个文件架构和文件夹,但最后出现错误,我不明白到底发生了什么以及有什么问题
"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
我的脚本 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
所以,正如我在评论中提到的,当 $item
是一个文件夹时,你引用了一个不存在的 属性,并试图调用它的方法,所以你会出错。不过,实际上,您这样做很困难。 PowerShell 将递归地为您复制内容,无需像您一样手动执行。与其编写自己的函数,不如这样做:
# 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