Jump to content
Sign in to follow this  
Dana L. Houseman

Pass $Var off to InputBox

Recommended Posts

Dana L. Houseman

I'd like to pass a Variable to an input Box, does anyone have a suggestion for my code?

Specif. Get the COMPUTER NAME, Prompt user for new name with old name in the INPUT box so the user can edit the old or enter a new name.

CODE
;Places the input box in the top left corner displaying the characters as they

;are typed.

$var = RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")

$answer = InputBox("My Rename Workstation", "What is the Name for this computer?", "", "", _

350, 100, 200, 200)

Send("{"& $var"}")

; Write a single REG_SZ value

RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", "REG_SZ", $answer)

TKS,

D~

Share this post


Link to post
Share on other sites
AcidCorps

;Places the input box in the top left corner displaying the characters as they
;are typed.
$var = RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")

$answer = InputBox("My Rename Workstation", "What is the Name for this computer?", $var, "", _
350, 100, 200, 200)

; Write a single REG_SZ value
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", "REG_SZ", $answer)

$var's don't work in quotes, simply put the $var where you would put the quotes and text

Share this post


Link to post
Share on other sites
Helge

Send would've been executed after Inputbox had been closed and not while it's still up, since it has

to wait for the user to input something. The way you've scripted it now requires Send to be executed

while the inputbox is still up but that the RegWrite however somehow magically knows that he should

wait.

Even though Send wouldn't have worked like you wanted it to, I would like to point out the proper way

of sending the value of the variable. Curly brackets are only for special keys so no need to use them

here (for example, {enter} equals the enter-key while "enter" simply equals the string as it is shown).

You also missed a ampersand after the variable.

Send($var)

Ok, this is just some extra information. For solving your problem look at AcidCorps' post :)

Share this post


Link to post
Share on other sites
Dana L. Houseman

;Places the input box in the top left corner displaying the characters as they
;are typed.
$var = RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")

$answer = InputBox("My Rename Workstation", "What is the Name for this computer?", $var, "", _
350, 100, 200, 200)

; Write a single REG_SZ value
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", "REG_SZ", $answer)

$var's don't work in quotes, simply put the $var where you would put the quotes and text

I changed it and the $var does not put the Current COMPUTERNAME in the INPUTBOX.

Share this post


Link to post
Share on other sites
herewasplato

I changed it and the $var does not put the Current COMPUTERNAME in the INPUTBOX.

You might want to manually check the value of the registry key that you have been reading from and writing to - it may be blank now.

Edit: Selecting cancel from the input box will make $answer = to an empty string - which the RegWrite gladly wrote to the key - subsequent runs of the script will look like the computer name is not being displayed as the default prompt in the input box... but it is :-)

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
Dana L. Houseman

You might want to manually check the value of the registry key that you have been reading from and writing to - it may be blank now.

Edit: Selecting cancel from the input box will make $answer = to an empty string - which the RegWrite gladly wrote to the key - subsequent runs of the script will look like the computer name is not being displayed as the default prompt in the input box... but it is :-)

Well that was a duh moment, because I had realized this before, but didn't think about it when I ran the above code.

Thanks for the reminder, it's working. Now a quick Question; Can I remove the CANCEL button, because I'd never want the end user to CANCEL the entry.

:)

Edited by Dana L. Houseman

Share this post


Link to post
Share on other sites
herewasplato

...I'd never want the end user to CANCEL the entry.

Try this loop. Pressing cancel causes it to just ask again.
$var = RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")

Do
    $answer = InputBox("My Rename Workstation", _
            "What is the Name for this computer?", _
            $var, "", 350, 100, 200, 200)
Until Not @error

; Write a single REG_SZ value
RegWrite("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", "REG_SZ", $answer)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites
herewasplato

You might also want to consider doing some error checking before you allow a user to write an invalid character to the registry.

$var = RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")
Do
    Do
        $answer = InputBox("My Rename Workstation", _
                "What is the Name for this computer?", _
                $var, "", 350, 100, 200, 200)
    Until Not @error ; I'm not sure that this is too kind

    $valid = False
    $bad = ""
    ;http://support.microsoft.com/kb/228275
    ;there might be more invalid characters
    If StringInStr($answer, "/", 1) Then $bad &= "/";slash
    If StringInStr($answer, "\", 1) Then $bad &= "\";backslash
    If StringInStr($answer, "[", 1) Then $bad &= "[";left bracket
    If StringInStr($answer, "]", 1) Then $bad &= "]";right bracket
    If StringInStr($answer, '"', 1) Then $bad &= '"';double quote
    If StringInStr($answer, ":", 1) Then $bad &= ":";colon
    If StringInStr($answer, ";", 1) Then $bad &= ";";semicolon
    If StringInStr($answer, "|", 1) Then $bad &= "|";pipe
    If StringInStr($answer, "<", 1) Then $bad &= "<";less than
    If StringInStr($answer, ">", 1) Then $bad &= ">";greater than
    If StringInStr($answer, "+", 1) Then $bad &= "+";plus
    If StringInStr($answer, "=", 1) Then $bad &= "=";equals
    If StringInStr($answer, ",", 1) Then $bad &= ",";comma
    If StringInStr($answer, "?", 1) Then $bad &= "?";question mark
    If StringInStr($answer, "*", 1) Then $bad &= "*";asterisk
    If StringInStr($answer, " ", 1) Then $bad &= " ";space
    If StringInStr($answer, "_", 1) Then $bad &= "_";underscore
    If StringInStr($answer, "", 1) Then $bad &= "";empty or null
    If Not $bad = "" Then
        MsgBox(0, "Error", "Invalid Character(s)" & @CR _
                 & ">>>" & $bad & "<<<")
        ContinueLoop
    Else
        $valid = True
    EndIf
Until $valid = True
ConsoleWrite(">>>" & $answer & "<<<" & @CR)
I've replaced the actual RegWrite with a ConsoleWrite for testing.

Now let's wait for a much smaller solution using StringRegExp to be posted by someone smarter than the MSP :-)


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×