#PowerShell code not working (sorry for non descriptive title xd i couldnt think of anything!)

52 messages · Page 1 of 1 (latest)

random galleon
#
Write-Host "Chipset drivers have finished installing."
Write-Host "Would you like to search for your motherboard's driver page to download any additional drivers?"

$key = Read-Host "[Y] Yes, search for motherboard page  [N] No, continue"
while (-not $validInput) {
    switch ($key.ToUpper()) {
        'Y' {
            $validInput = $true
            $searchUrl = "https://duckduckgo.com/?q=motherboard+drivers+for+$($fullMotherboardName -replace ' ', '+')"
            Read-Host
            Start-Process $searchUrl
        }
        'N' {
            $validInput = $true
        }
        default {
            $validInput = $false
            Write-Host "Invalid input. Please try again"
        }
    }
}

Y and N do the same thing and "default" / invalid input loops forever
This code was about 50% ai generated so i didn't really expect it to work great/at all

#

do i need to add break(s)?

#

if so, where

formal forum
#

Ideally you would switch over to use param()

#

And you can use ValidateSet attribute.

#

Aside from all that, I can't figure out what you wan to happen. It looks ok to me.

#

I would define $validInput = $false before the while Maybe that doesn't matter.

random galleon
formal forum
#

Well that is probably harder than fixing what you have now. What is the issue with the script now?

#

The additional read-host that you have is probably messing up too.

random galleon
# formal forum Well that is probably harder than fixing what you have now. What is the issue wi...

Well the issues before were:

  1. pressing keys "y" and "n" do the same thing, when "y" should open that page defined in the block of code below in this message
  2. when you press an invalid key, it infinitely loops "Invalid input. Please try again."

Now, instead of the #2 issue of the infinite loop, it's just exiting, but the intended behavior is to send the invalid message once and restart the loop to give the user another chance to press a valid key.

Also this code was missing from my message, it's before everything i wrote there:

# Get board and define variable
$board = (Get-WmiObject Win32_BaseBoard).Product
$manufacturer = (Get-WmiObject Win32_BaseBoard).Manufacturer
$fullMotherboardName = $manufacturer + " " + $board
formal forum
#

Is this script a personal thing or something for open source or for an enterprise?

random galleon
#

so yeah that's open-source

formal forum
#

Is it just in a file.ps1 file that is called?

random galleon
formal forum
#

So then you need the add the $key= line after your write-host

random galleon
#

But for testing i've been copying the code into powershell if that changes anything

formal forum
#

In the default for the switch

formal forum
random galleon
#

and where do i move the $key= line

#

I would think you want to define if the pressed key was a valid input or not after the key is pressed and detected

#

if it's y or n, it's valid, if anything else, invalid

formal forum
random galleon
#

Oh
so i just add another validinput=false before? got it

formal forum
random galleon
#

will this fix the "y doing same as n" issue?

#

@formal forum using this now and getting error:

You cannot call a method on a null-valued expression.
At C:\Users\charl\Downloads\test.ps1:10 char:1
+ $validInput = $false
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
#

i also tried that validinput line before the while statement, same error

formal forum
#
Write-Host "Chipset drivers have finished installing."
Write-Host "Would you like to search for your motherboard's driver page to download any additional drivers?"

$validInput = $false
$key = Read-Host "[Y] Yes, search for motherboard page  [N] No, continue"
while (-not $validInput) {
    switch ($key.ToUpper()) {
        'Y' {
            $validInput = $true
            $searchUrl = "https://duckduckgo.com/?q=motherboard+drivers+for+$($fullMotherboardName -replace ' ', '+')"
            Start-Process $searchUrl
        }
        'N' {
            $validInput = $true
        }
        default {
            $validInput = $false
            Write-Host "Invalid input. Please try again"
            $key = Read-Host "[Y] Yes, search for motherboard page  [N] No, continue"
        }
    }
}

like this

#

edited it. you have a lone read-host

#

ok this is it.

# Get board and define variable
$board = (Get-CimInstance Win32_BaseBoard -Property Product).Product
$manufacturer = (Get-CimInstance Win32_BaseBoard -property Manufacturer).Manufacturer
$fullMotherboardName = $manufacturer + " " + $board
Write-Host "Chipset drivers have finished installing."
Write-Host "Would you like to search for your motherboard's driver page to download any additional drivers?"

$validInput = $false
$key = Read-Host "[Y] Yes, search for motherboard page  [N] No, continue"
while (-not $validInput) {
    switch ($key.ToUpper()) {
        'Y' {
            $validInput = $true
            $searchUrl = "https://duckduckgo.com/?q=motherboard+drivers+for+$($fullMotherboardName -replace ' ', '+')"
            Start-Process $searchUrl
        }
        'N' {
            $validInput = $true
        }
        default {
            $validInput = $false
            Write-Host "Invalid input. Please try again"
            $key = Read-Host "[Y] Yes, search for motherboard page  [N] No, continue"
        }
    }
}
random galleon
#

oh yeah i forgot wmiobject was phasing out/deprecated

#

i'll try this 1 sec

random galleon
formal forum
# random galleon thank you so much it works!

this is a simpler way to do it.

[CmdletBinding()]
param (
    [Parameter(Mandatory,HelpMessage='[Y] Yes, search for motherboard page  [N] No, continue')]
    [ValidateSet('Y','N')]
    [string]
    $YOrN
)

switch ($YOrN) {
    'Y' {
        # Get board and define variable
        $base = Get-CimInstance Win32_BaseBoard -Property Product,Manufacturer
        $fullMotherboardName = "$($base.Product)+$($base.Manufacturer)"
        Start-Process "https://duckduckgo.com/?q=motherboard+drivers+for+$fullMotherboardName"
    }
}
random galleon
#

I probably prefer the first solution though since i understand it more xd

formal forum
#
[CmdletBinding()]
param (
    [Parameter(Mandatory,HelpMessage='[Y] Yes, search for motherboard page  [N] No, continue')]
    [ValidateSet('Y','N')]
    [string]
    $YOrN
)

if ($YOrN -eq 'Y') {
    # Get board and define variable
    $base = Get-CimInstance Win32_BaseBoard -Property Product,Manufacturer
    $fullMotherboardName = "$($base.Product)+$($base.Manufacturer)"
    Start-Process "https://duckduckgo.com/?q=motherboard+drivers+for+$fullMotherboardName"
}

even simpler

formal forum
#

I'm mostly against the whole read-host hand holding in certain CLI programs though I see where some people might like it. write the loop and such just gets more complicated.

random galleon
#

because sometimes i might do something like this

Write-Host "press any key to <action>"
Read-Host
<command>
formal forum
#

yeah that is the way to do it if you are making this kind of cli app.

formal forum
#

if you are dealing with novice users, then i can see it maybe.

#

I would just use param() and provide help for whatever they want.

#

powershell on its own has tab-completion and plenty of help if folks know where to look.

#

hard for me to say to to much without seeing how this snippet fits into the greater app.