我想为 mdt 创建一个 powershell 安装程序/卸载程序,但我最终遇到以下错误。
代码工作正常,它可以很好地复制目标中的整个文件架构和文件夹,但最后出现错误,我不明白到底发生了什么以及问题是什么
"Impossible d'appeler une méthode dans une expression Null.">
"无法在 Null 表达式中调用方法。">
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。目录名称.替换($fromFolder,$toFolder( + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息 : 无效操作 : (:) [], 运行时异常 + FullQualifiedErrorId : 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 + 如果 (!(测试路径($dir((( + ~~~~~~ + 类别信息 : 无效数据 : (:) [测试路径], 参数绑定验证异常 + FullQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Command.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++HiddenNotepad++"
$SourcePluginNotepadPlusPlus = "$(Get-Location)ComparePlugin"
$DestinationLanguageNotepadPlusPlus = "C:UsersDefaultAppDataRoamingNotepad++"
$DestinationPluginNotepadPlusPlus = "C:Program FilesNotepad++pluginsComparePlugin"
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