The other day I gave you a function to create a VBScript style message box in PowerShell. If you find yourself needing MsgBox in PowerShell, you’re likely going to want InputBox. This too is accomplished using the [microsoft.visualbasic.interaction] class. You’ll need to load the assembly first if you haven’t already.
PS C:\> [reflection.assembly]::loadwithpartialname(“microsoft.visualbasic”) | Out-Null
PS C:\> [microsoft.visualbasic.interaction]::Inputbox(“enter something”)
As in VBScript you can also specify a title and a default value. Here’s a more complete function you could call and an example.
1: Function Show-Inputbox {
2: Param([string]$message=$(Throw "You must enter a prompt message"),
3: [string]$title="Input",
4: [string]$default
5: )
6:
7: [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
8: [microsoft.visualbasic.interaction]::InputBox($message,$title,$default)
9:
10: }
11:
12: $c=Show-Inputbox -message "Enter a computername" `
13: -title "Computername" -default $env:Computername
14:
15: if ($c.Trim()) {
16: Get-WmiObject win32_computersystem -computer $c
17: }
Executing the sample code will give you this inputbox:
The inputbox’s return value is whatever you enter. What you need to do, as I’ve done, is add code to validate the data.
$c=Show-Inputbox -message “Enter a computername” `
-title “Computername” -default $env:Computername
if ($c.Trim()) {
Get-WmiObject win32_computersystem -computer $c
}
In my example I only use the value in the Get-WMIObject expression if $c has a value. I used the Trim() method to remove any spaces. It’s possible a user could accidentally enter space somewhere so using Trim cleans that up.
Download this sample here
FYI, SAPIEN’s PowerShell extensions snap-in has a Read-InputBox cmdlet, too. Same basic effect, and an easy way to see how to write cmdlets since the source code’s simplistic.
I probably should have mentioned that. For those interested, you can download SAPIEN’s Powershell extensions at http://www.primalscript.com/Free_Tools/index.asp.
I am using this function along with other things to pull the BIOS information for remote computers. The only problem I am having is making the InputBox show on top of other windows after being ran. It jumps behind everything else. Is there a way to force the box in front?
This is the script I’m using..still a newb so don’t really know how to clean it up. Thanks for any help and sorry for bringing one back from the past.
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
}
foreach ($strComputer in $arrComputers)
{
$colItems = get-wmiobject -class “Win32_BIOS” -namespace “root\CIMV2” `
-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
}
}
I haven’t found a good way to force a form to get focus. It would be better if you could post your script and problem in the PowerShell forum at ScriptingAnswers.com. Much easier to work on a problem there versus blog comments.
Will do thanks!