Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

If statement question

$
0
0

Would anyone know why my function works except for the first "If" statement I have in the process block?I swear all of this worked last night...

#Gets Java version from a computer list entered by parameter or .csv path

function GetJavaVersionWork {
  param([string]$ComputerName)

    Try
    {
      Write-Host "Checking computers for Java versions on $Computer.." -ForegroundColor Cyan     
      
        Invoke-Command -ComputerName $Computer -ThrottleLimit 100 -ScriptBlock {

        
          $ComputerName = gwmi win32_operatingsystem -Property csname | select -expand csname      
          $ChkJavaVersion = gwmi win32_product -Filter "name like '%java%'" | select -expand version


          
                if ($ChkJavaVersion -eq $Null)
                {

                    Write-Host "Java is not installed on $ComputerName" -ForegroundColor Red
          
                }


          $Obj = New-Object -TypeName psobject
          $Obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $ComputerName
          $Obj | Add-Member –membertype NoteProperty –name "Java Version" -value $ChkJavaVersion

                


          $Obj

        
        } -ea Stop | select 'Computer' , 'Java Version' | Export-Csv C:\JavaList.csv -Append

    }
        
       
      
    Catch
    {
          
      Write-Host "Logging error to file.." -ForegroundColor Red
      $_ | Out-File c:\Errors.txt -Append
        
    }


}


function Get-JavaVersion {
  [CmdletBinding()]
  param (
          [string[]]$ComputerName,
          [string]$CSVPath
)

      
      BEGIN {
        DEL c:\JavaList.csv -ea SilentlyContinue
        DEL c:\JavaList.txt -ea SilentlyContinue
        DEL c:\errors.txt -ea SilentlyContinue
      }
 
    
    
      PROCESS {


    
      if (($ComputerName -eq $Null) -and ($CSVPath -eq $Null))
      {
          
           Throw New-Object System.ArgumentException("You MUST enter a computer name OR a .csv path")
      
      }


             elseif (($ComputerName -eq $Null) -and ($CSVPath -ne $Null)) {
                
               
                    $ComputerList = Import-Csv $CSVPath | select -ExpandProperty ComputerName
                 
                    
                    foreach ($Computer in $ComputerList) {
                    
                        
                       if (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
                       {
              
                           GetJavaVersionWork -ComputerName $Computer
          
                       }


                       
                      else
                      {
             
                          Write-Host "Cannot connect to $Computer. Logging $Computer to file.." -ForegroundColor Red
                          $Computer | Out-File c:\comps.txt -Append
          
                      }

            
                 }

           
            }

      
            
             elseif ($ComputerName -ne $Null) {         
       
        
                foreach ($Computer in $ComputerName) {  
       
                
                  if (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
                  {
              
                      GetJavaVersionWork -ComputerName $Computer
          
                  }
           
          
                  else
                  {
             
                      Write-Host "Cannot connect to $Computer. Logging $Computer to file.." -ForegroundColor Red
                      $Computer | Out-File c:\comps.txt -Append
          
                  }
   
            }
 
       }

  }

 
    END {
      Write-Host "Operation complete!" -ForegroundColor Yellow
    }

}
Get-JavaVersion


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>