Tip to create a check on validity on variable input

This topic contains 5 replies, has 3 voices, and was last updated by  Christian 2 months, 3 weeks ago.

  • Author
    Posts
  • #94461

    Christian
    Participant

    Hi all,

    Since I'm a noob with PowerShell I'm pretty happy with what I managed to do this morning. But I want to take things a level further.

    So what I did is the following. I created a script that shows me all processes with CPU util less than 100% and should sort it. Then I decided it would be nicer to just see a process name. So I added that. However, I wanted to be able to search by wildcards. Which works when the process exists. When I enter complete and utter rubbish it shows this as screen output. How can I put a validation tool in place that tells me that the name couldn't be found?

    Script here:

    cls;
    #Now we try the same thing with providing a process name and run the same script again :)))
    #
    #This tool will give you more information on a process you chose. Please make sure you type the process name exactly or use the wildcard * to find the service you want to check on. PowerShell rocks!
    $process = get-process 
    [string]$process=Read-host "Enter process name"
    $process | where -Property ProcessName -like "*" | where -Property CPU -le 100 | sort -Property CPU -Descending
    get-process $process
    

    A hint would be good the rest I want to discover but my searches so far didn't yield any result. No solution just hints please.

    Update: I think it half way works:

    #This is to play around to see if I can get a validation in place so no gibberish can be returned on screen
    #
    cls;
    #Now we try the same thing with providing a process name and run the same script again :)))
    #
    #This tool will give you more information on a process you chose. Please make sure you type the process name exactly or use the wildcard * to find the service you want to check on. PowerShell rocks!
    $process = get-process 
    [string]$process=Read-host "Enter process name"
    $process | where -Property ProcessName -like $process | where -Property Name -like "*" |  where -Property CPU -le 100 | sort -Property CPU -Descending |  write-host "Invalid process!"
    get-process $process

    But I can't get the write-host to show up. It correctly says that service name couldn't be found via PowerShell standard output.

  • #94467

    Ben
    Participant

    I think you need to try at first to use an if..else statement. When I run your code if it works it shows me the Process name that I entered. If it is invalid then use your write-host message instead of the default error message.

  • #94470

    Shihan Pietersz
    Participant

    Hi Christian,

    you can try the below variation

     $processName = Read-Host -Prompt "Enter Process Name"
     $Process = Get-Process $processName -ErrorAction SilentlyContinue
     if($Process.ProcessName -and $_.CPU -le 100){$Process.ProcessName, $Process.CPU | sort -Property CPU -Descending }else{Write-Host "Invalid Process"}
    

    regards
    Shihan

  • #94476

    Christian
    Participant

    Hi Shihan,

    I tried your code but it returns invalid code, I believe that's because of the condition statement of

    $Process.ProcessName -and $_.CPU -le 100

    I actually got it to work with

    #This is to play around to see if I can get a validation in place so no gibberish can be returned on screen
    #
    cls;
    $process = get-process 
    [string]$process=Read-host "Enter process name"
    $process | where -Property ProcessName -like $process | where -Property Name -like "*" |  where -Property CPU -le 100 | sort -Property CPU -Descending 
    write-host "Invalid process!" 
    get-process $process
    

    When I enter wildcards and process not existing getting error. When entering correctly it works.

    I think in your code it misses a condition for the if clause but not sure how to fix it.

  • #94479

    Shihan Pietersz
    Participant

    HI Christian,
    The $Process.ProcessName will only be true if the $Process variable contains a value else it will be false. that is what the IF condition is checking.

    you would get invalid process if the process name does not exist. when i run the above code in the console works fine. see output with sample below

    $processName = Read-Host -Prompt "Enter Process Name"
    Enter Process Name: Powershell
    $Process = Get-Process $processName -ErrorAction SilentlyContinue
    if($Process.ProcessName -and $Process.CPU -le 100){$Process.ProcessName, $Process.CPU | sort -Property CPU
     -Descending }else{Write-Host "Invalid Process"}
    17.34375
    powershell
    

    Regards
    Shihan

  • #94810

    Christian
    Participant

    Hi Shihan,

    Many thanks I checked my code and didn't make sense. I'm trying your script again. Thank you very much for taking the time and providing me with some code.

    Have a great day.

    Chris

You must be logged in to reply to this topic.