Jump to content

Recommended Posts

Posted

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;}

Posted

$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 - - -

Posted

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!

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...