Solved

Search for Text String

Posted on 2011-03-16
5
360 Views
Last Modified: 2012-05-11
I have the following script
==============================================================
Clear
Function check-remotefile {
    PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
$file = "\\$_\\c$\pos\bin\javaenv_version.bat"

If (Get-Content $file -erroraction silentlycontinue|  Where-Object { $_ -like 'set javaenv_version=2.9' })        
                  {          
                        echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
                  }
              Else
                  {
                        echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
                  }
                  }
           
    Else{
            $_ + ",OFFLINE ,Firmware Version Not Checked"
        }
        }    
   }
Get-Content  c:\ingver.txt | check-remotefile

==============================================================

Right now if the file javaenv_version.bat  is not  on the pc at all it reports it as "not found"because it can not locate the text . How can get it to tell me the file is not found as well ?
0
Comment
Question by:jfgray
  • 3
  • 2
5 Comments
 

Author Comment

by:jfgray
ID: 35150489
If I remove the -erroraction silentlycontinue

It will give me an error

Get-Content : Cannot find path '\\P**********\\c$\pos\bin\javaenv_version.bat' because it does not exist.
At C:\pshell\Ingenico_Version 2.ps1:7 char:16
+ If (Get-Content <<<<  $file |  Where-Object { $_ -like 'set javaenv_version=2.9' })        
    + CategoryInfo          : ObjectNotFound: (\\**********\\c...env_version.bat:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

I would like it to say "PCNAME,ONLINE, File Not Found"
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35150624
I would use the test-path cmdlet.
Clear
Function check-remotefile {
PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat" 
        if (!(Test-Path -Path $file)){
            echo "$_,ONLINE,file not found"
        }
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
        }
        else {
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
        }
    Else {
        "$_,OFFLINE ,Firmware Version Not Checked"
    }
}    
}
Get-Content  c:\ingver.txt | check-remotefile

Open in new window

0
 

Author Comment

by:jfgray
ID: 35150715
I recieved the following error

The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At C:\Users\jg199\AppData\Local\Temp\ed7aba1e-cfdc-4875-8026-17b709a9e31b.ps1:15 char:9
+     Else <<<< 
    + CategoryInfo          : ObjectNotFound: (Else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Here is the script as I ran it

Clear
Function check-remotefile {
PROCESS {
    If (Test-Connection -ComputerName "$_" -Quiet){
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat"
        if (!(Test-Path -Path $file)){
            echo "$_,ONLINE,file not found"
        }
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"
        }
        else {
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"
        }
    Else
      {
        "$_,OFFLINE ,Firmware Version Not Checked"
    }
}    
}
}
Get-Content  c:\ingver.txt | check-remotefile



0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35150779
Sorry, one bracket is missing. Here is hopefully the correct one.
Clear  
Function check-remotefile {  
PROCESS {  
    If (Test-Connection -ComputerName "$_" -Quiet){  
        $file = "\\$_\\c$\pos\bin\javaenv_version.bat"   
        if (!(Test-Path -Path $file)){  
            echo "$_,ONLINE,file not found"  
        }  
        elseif(Select-String -Path $file -Pattern "set\sjavaenv_version=2\.9"){  
            echo "$_,ONLINE,Ingenico Firmware 2.9 Found"  
        }  
        else {  
            echo "$_,ONLINE,Ingenico Firmware 2.9 Not Found"  
        }
	}
    Else {  
        "$_,OFFLINE ,Firmware Version Not Checked"  
    }  
}      
}  
Get-Content  c:\ingver.txt | check-remotefile

Open in new window

0
 

Author Comment

by:jfgray
ID: 35283159
I thought I closed this sorry for the delay.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question