Uber22 Posted March 2, 2021 Posted March 2, 2021 Hey Am after help with a script that i am scratching my head trying to get it working. i just get an error message saying ('Ninite+Agent' NOT is installed.Starting downloadtmp --> +c:\Windows\System32\Ninite+Agent.msiTried installing ) Here is my script. $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 $soft_name" } else { Write-Host "'$software' is already installed." }}catch { $ErrorMessage = $_.Exception.Message Write-Host "An error occurred." Write-Host $ErrorMessage;}
bald_pig Posted March 2, 2021 Posted March 2, 2021 Can you use the code tags to keep the formatting? Makes it a lot easier to look at!
bald_pig Posted March 2, 2021 Posted March 2, 2021 Actually, that output suggests that the script is running as intended, that's what is being written to screen when installing.
Uber22 Posted March 2, 2021 Author Posted March 2, 2021 $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 $soft_name" } else { Write-Host "'$software' is already installed." }}catch { $ErrorMessage = $_.Exception.Message Write-Host "An error occurred." Write-Host $ErrorMessage;} - - - Updated - - -
morboss Posted March 2, 2021 Posted March 2, 2021 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!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now