InputBox Function

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jared572
Posts: 13
Last visit: Thu Apr 04, 2013 1:21 pm

InputBox Function

Post by jared572 »

I am trying to use an InputBox to enter data into a script I have the function working but there is one small problem. When the script executes the input box comes up behind all other windows. I am looking for a way to have the Box come up in front of everything. I did find the code below but do not know how to insert it into my script.

This needs be inserted into the script below.

Code: Select all

$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")

SCRIPT

Code: Select all

Function Show-Inputbox {
 Param([string]$message=$(Throw "You must enter a prompt message"),
       [string]$title="Input",
       [string]$default
       )
       
 [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
 [microsoft.visualbasic.interaction]::InputBox($message,$title,$default)
 
}
$c=Show-Inputbox -message "Enter a computername" `
-title "Computername" -default $env:Computername
if ($c.Trim()) {
  Get-WmiObject win32_computersystem -computer $c
  }
 
 
    $colItems = get-wmiobject -class "Win32_BIOS" -namespace "rootCIMV2" `
    -computername $c
   foreach ($objItem in $colItems)
   {
     Write-host "Computer Name: " $c
      write-host "BIOS Version: " $objItem.BIOSVersion
      write-host "Build Number: " $objItem.BuildNumber
      write-host "Caption: " $objItem.Caption
      write-host "Code Set: " $objItem.CodeSet
      write-host "Current Language: " $objItem.CurrentLanguage
      write-host "Description: " $objItem.Description
      write-host "Identification Code: " $objItem.IdentificationCode
      write-host "Installable Languages: " $objItem.InstallableLanguages
      write-host "Installation Date: " $objItem.InstallDate
      write-host "Language Edition: " $objItem.LanguageEdition
      write-host "List Of Languages: " $objItem.ListOfLanguages
      write-host "Manufacturer: " $objItem.Manufacturer
      write-host "Name: " $objItem.Name
      write-host "Other Target Operating System: " $objItem.OtherTargetOS
      write-host "Primary BIOS: " $objItem.PrimaryBIOS
      write-host "Release Date: " $objItem.ReleaseDate
      write-host "Serial Number: " $objItem.SerialNumber
      write-host "SMBIOS BIOS Version: " $objItem.SMBIOSBIOSVersion
      write-host "SMBIOS Major Version: " $objItem.SMBIOSMajorVersion
      write-host "SMBIOS Minor Version: " $objItem.SMBIOSMinorVersion
      write-host "SMBIOS Present: " $objItem.SMBIOSPresent
      write-host "Software Element ID: " $objItem.SoftwareElementID
      write-host "Software Element State: " $objItem.SoftwareElementState
      write-host "Status: " $objItem.Status
      write-host "Target Operating System: " $objItem.TargetOperatingSystem
      write-host "Version: " $objItem.Version
      write-host
   }


Any and all help is appreciated. Thanks!
User avatar
jared572
Posts: 13
Last visit: Thu Apr 04, 2013 1:21 pm

InputBox Function

Post by jared572 »

I am trying to use an InputBox to enter data into a script I have the function working but there is one small problem. When the script executes the input box comes up behind all other windows. I am looking for a way to have the Box come up in front of everything. I did find the code below but do not know how to insert it into my script.

This needs be inserted into the script below.

Code: Select all

$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")

SCRIPT

Code: Select all

Function Show-Inputbox {
 Param([string]$message=$(Throw "You must enter a prompt message"),
       [string]$title="Input",
       [string]$default
       )
       
 [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
 [microsoft.visualbasic.interaction]::InputBox($message,$title,$default)
 
}
$c=Show-Inputbox -message "Enter a computername" `
-title "Computername" -default $env:Computername
if ($c.Trim()) {
  Get-WmiObject win32_computersystem -computer $c
  }
 
 
    $colItems = get-wmiobject -class "Win32_BIOS" -namespace "rootCIMV2" `
    -computername $c
   foreach ($objItem in $colItems)
   {
     Write-host "Computer Name: " $c
      write-host "BIOS Version: " $objItem.BIOSVersion
      write-host "Build Number: " $objItem.BuildNumber
      write-host "Caption: " $objItem.Caption
      write-host "Code Set: " $objItem.CodeSet
      write-host "Current Language: " $objItem.CurrentLanguage
      write-host "Description: " $objItem.Description
      write-host "Identification Code: " $objItem.IdentificationCode
      write-host "Installable Languages: " $objItem.InstallableLanguages
      write-host "Installation Date: " $objItem.InstallDate
      write-host "Language Edition: " $objItem.LanguageEdition
      write-host "List Of Languages: " $objItem.ListOfLanguages
      write-host "Manufacturer: " $objItem.Manufacturer
      write-host "Name: " $objItem.Name
      write-host "Other Target Operating System: " $objItem.OtherTargetOS
      write-host "Primary BIOS: " $objItem.PrimaryBIOS
      write-host "Release Date: " $objItem.ReleaseDate
      write-host "Serial Number: " $objItem.SerialNumber
      write-host "SMBIOS BIOS Version: " $objItem.SMBIOSBIOSVersion
      write-host "SMBIOS Major Version: " $objItem.SMBIOSMajorVersion
      write-host "SMBIOS Minor Version: " $objItem.SMBIOSMinorVersion
      write-host "SMBIOS Present: " $objItem.SMBIOSPresent
      write-host "Software Element ID: " $objItem.SoftwareElementID
      write-host "Software Element State: " $objItem.SoftwareElementState
      write-host "Status: " $objItem.Status
      write-host "Target Operating System: " $objItem.TargetOperatingSystem
      write-host "Version: " $objItem.Version
      write-host
   }


Any and all help is appreciated. Thanks!
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

InputBox Function

Post by jhicks »

Why are you adding that block of code? You shouldn't need it. Also you're code isn't quite correct. It should be thisif ($c.Trim()) { $colItems = get-wmiobject -class "Win32_BIOS" -namespace "rootCIMV2" ` -computername $c foreach ($objItem in $colItems) { Write-Host "Computer Name: " $c Write-Host "BIOS Version: " $objItem.BIOSVersion Write-Host "Build Number: " $objItem.BuildNumber Write-Host "Caption: " $objItem.Caption Write-Host "Code Set: " $objItem.CodeSet write-host "Current Language: " $objItem.CurrentLanguage Write-Host "Description: " $objItem.Description write-host "Identification Code: " $objItem.IdentificationCode write-host "Installable Languages: " $objItem.InstallableLanguages Write-Host "Installation Date: " $objItem.InstallDate Write-Host "Language Edition: " $objItem.LanguageEdition Write-Host "List Of Languages: " $objItem.ListOfLanguages Write-Host "Manufacturer: " $objItem.Manufacturer Write-Host "Name: " $objItem.Name Write-Host "Other Target Operating System: " $objItem.OtherTargetOS Write-Host "Primary BIOS: " $objItem.PrimaryBIOS Write-Host "Release Date: " $objItem.ReleaseDate Write-Host "Serial Number: " $objItem.SerialNumber Write-Host "SMBIOS BIOS Version: " $objItem.SMBIOSBIOSVersion Write-Host "SMBIOS Major Version: " $objItem.SMBIOSMajorVersion Write-Host "SMBIOS Minor Version: " $objItem.SMBIOSMinorVersion Write-Host "SMBIOS Present: " $objItem.SMBIOSPresent Write-Host "Software Element ID: " $objItem.SoftwareElementID Write-Host "Software Element State: " $objItem.SoftwareElementState Write-Host "Status: " $objItem.Status Write-Host "Target Operating System: " $objItem.TargetOperatingSystem Write-Host "Version: " $objItem.Version Write-Host } }Although even that really isn't the PowerShell way. It looks like you are still transitioning from VBScript. There's nothing necessarily wrong with this but you aren't really using the PowerShell paradigm.Without the added code I get the input box on top.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

InputBox Function

Post by jhicks »

You don't even really need a script. Assuming the function is loaded into PowerShell:show-inputbox "Enter a computername" | foreach {gwmi win32_bios -comp $_ | select *}
User avatar
jsel
Posts: 2
Last visit: Mon May 11, 2009 6:43 pm

InputBox Function

Post by jsel »

I seem to miss something here.When I just load the function 'show-inputbox' into PowerShell and then callshow-inputbox "Enter a computername" | foreach {gwmi win32_bios -comp $_ | select *}the inputbox is in the background, not on top.Could you explain this to me?Many thanks,Joerg
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

InputBox Function

Post by jhicks »

I think that's a limitation of the InputBox() method. I don't think there's a way to bring it to the foreground, at least not easily. What you might prefer instead is to make this entire script as Windows Form based script. The free PrimalForms app can make this easier to script.
User avatar
jared572
Posts: 13
Last visit: Thu Apr 04, 2013 1:21 pm

InputBox Function

Post by jared572 »

I ended up using the following script. It does what I want it to and the InputBox does show up in the foreground.

Code: Select all

	
$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Enter ComputerName`",`"ComputerName`") end function" )
$c = $a.eval("getInput")
    
	
if ($c.Trim()) {
      Get-WmiObject win32_computersystem -computer $c
  
    $colItems = get-wmiobject -class "Win32_BIOS" -namespace "rootCIMV2" `
     -computername $c
	

    $IPAddress = get-wmiobject -class "Win32_NetworkAdapterConfiguration" -namespace "rootCIMV2" `
-computername $c -filter "IPEnabled = true"
	

   foreach ($objItem in $colItems)
   {
      Write-host "Computer Name       :" $c
      write-host "Manufacturer        :" $objItem.Manufacturer
      write-host "Serial Number       :" $objItem.SerialNumber
      write-host "SMBIOS BIOS Version :" $objItem.SMBIOSBIOSVersion
      write-host "-------------------------------------------------------------"
   }
   foreach ($objItem in $IPAddress)
   {
      write-host "MACAddress          :" $objItem.MACAddress
      write-host "IPAddress           :" $objItem.IPAddress
      write-host "-------------------------------------------------------------"
}
 }
	
 
	
jared5722009-05-11 05:55:02
User avatar
joel.delatorre
Posts: 83
Last visit: Tue Jan 17, 2017 9:26 am

InputBox Function

Post by joel.delatorre »

This code is from MOW's blog and can also be found on the SCripting Guys website.

Code: Select all


$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")





joel.delatorre2009-05-11 07:47:04
User avatar
jsel
Posts: 2
Last visit: Mon May 11, 2009 6:43 pm

InputBox Function

Post by jsel »

I don't think I asked this before, but why even bother with a GUI input box? You can keep everything in the console by using Read-Host.Yes, I could, but you know, how users are. They don't like command lines.Also, I noticed this behaviour on every messagebox I've created and a solution would come in handy.Joerg
This topic is 14 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked