none
Powershell script help needed (createing variables with user input) RRS feed

  • Question

  • Hello, 

    I have built a powershell script that we are using to gether asset information. Part of the script requires the user to enter asset information (name, asset tag and serialnumber) manually.  this is done for monitors, deskphones etc.   All of this information is stored in varialbes, then emailed to our ticketing system. 

    Currently I am prompting users for assets with the below; then just pumping those into the body of the email. 

    $Asset1 = Read-Host -Prompt 'Input Asset one information'
    $Asset2 = Read-Host -Prompt 'Input Asset two information'
    $Asset3 = Read-Host -Prompt 'Input Asset three information'
    $Asset4 = Read-Host -Prompt 'Input Asset four information'
    $Asset5 = Read-Host -Prompt 'Input Asset five information'

    This is not verry efficent or elegant!   Most of our users have differing numbers of assets.  Some might have 5 others might have 2, etc.  

    I would like the script to bring up a dialoge box and ask the user to input asset information and store it in a variable.  It would then have an "add" option that would create a new variable and store the information in it.  That is currently above my skill level and understanding.  

    Even when I get this to work; Then the question of how to display those variables in the email body. Would I just store those variables in an array? then have the array print out in the message body.  How would the array know about variables that do not exist before the user creates them.  

    Please let me know if more information is needed and thank you for any help you can provide!


    Tuesday, June 12, 2018 4:23 PM

Answers

  • Use a mandatory array parameter. The user can then enter the values directly on the PowerShell command line, or PowerShell will prompt for the parameter values. Example:


    param(
      [Parameter(Mandatory = $true)]
      [String[]] $Asset
    )
    
    "You entered: $Asset"
    

    The $Asset variable will contain an array.

    If you don't understand this answer, then we would very much recommend starting with some good beginner PowerShell tutorials. The Learn link at the top of this page is a good starting point.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 12, 2018 4:45 PM
    Moderator

All replies

  • Use a mandatory array parameter. The user can then enter the values directly on the PowerShell command line, or PowerShell will prompt for the parameter values. Example:


    param(
      [Parameter(Mandatory = $true)]
      [String[]] $Asset
    )
    
    "You entered: $Asset"
    

    The $Asset variable will contain an array.

    If you don't understand this answer, then we would very much recommend starting with some good beginner PowerShell tutorials. The Learn link at the top of this page is a good starting point.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 12, 2018 4:45 PM
    Moderator
  • The below section of code would allow for a variation in number of assets via command line input.  The assets will be contained in the $assetArray variable for use later in your script.  If you are looking into creating a graphical interface I would recommend looking into "Windows Forms" in PowerShell.  Hope this helps.

    $assetArray = @();
    $inputContinue = $True;
    While($inputContinue -eq $True){
    	$currentAsset = "";
    	$confirmContinue = "y";
    	$currentAsset = Read-Host "Input Asset Information";
    	##Add entered asset to array
    	$assetArray += $currentAsset;
    	##Confirm user wants to add additional assets
    	$confirmContinue = Read-Host "Add additional asset? Y/n"
    	if($confirmContinue -eq "n"){
    		$inputContinue = $False;
    	}
    }
    Write-Output $assetArray;



    Wednesday, June 13, 2018 7:19 AM
  • @AutomatedPenguin - a few points on your code:

    * You don't need trailing ; characters after lines of code in PowerShell

    * The code could be shortened by using do {} while or do {} until

    * The code doesn't check for empty entries

    But this is all a bit beside the point -- why write custom code when PowerShell can do it for you? (I don't see the point of reinventing the wheel.)


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 13, 2018 2:27 PM
    Moderator
  • @Bill_Stewart - Thank you for the advice.
    The use of semi colons is just a preference of mine.  As to the re-inventing the wheel, I was not aware of that feature.  Thank you.

    Wednesday, June 13, 2018 5:47 PM
  • I refer the honorable gentleman to my first post in this thread.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 13, 2018 9:20 PM
    Moderator