As the previous poster @bald_pig has already pointed out, we need to be able to read your code to help you.
You have given your us code in code tags but put it all on one line. However, I've managed to work out that your code should look something like this:
$appToMatch = '*Ninite Agent*';
function Get-InstalledApps {
if ([intPtr]::Size -eq 4) {
$regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
}
else{
$regpath = @( 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' ) }
Get-ItemProperty $regpath | . { process { if ($_.DisplayName -and $_.UninstallString) { $_ } } } | Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString | Sort DisplayName
}
try {
$file = 'Ninite+Agent.msi'
$link = "https://niniteice.s3.eu-west-2.amazonaws.com/Ninite+Agent.msi"
$software = 'Ninite+Agent'
$result = Get-InstalledApps | Where-Object { $_.DisplayName -like $appToMatch }
If ($result -eq $null) {
Write-Host "'$software' NOT is installed.";
Write-Host "Starting download";
$tmp = "c:\Windows\System32\$file"
Write-Host "tmp --> "+$tmp;
$client = New-Object System.Net.WebClient
$client.DownloadFile($link, $tmp)
msiexec /i $tmp /qn Remove-Item $tmp
Write-Host "Tried installing $software" # $soft_name has no value so will come up blank
} else {
Write-Host "'$software' is already installed."
}
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Host "An error occurred."
Write-Host $ErrorMessage
}
It all seems to be working correctly.
The only thing is that you had the unassigned variable $soft_name in the "Tried installing" message. So instead I have used $software variable...
Write-Host "Tried installing $software"
I hope that helps!