read-host

This topic contains 9 replies, has 4 voices, and was last updated by  Leon P 2 weeks, 5 days ago.

  • Author
    Posts
  • #90647

    Leon P
    Participant

    Hello all,

    This is an easy one and just me being stupid, i am running the below but for some reason now i have used mandatory=$true it now longer adds the message section.

    This is the parameter i am using,

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)]
    [String] $UpdateDay = (Read-Host -Prompt 'Enter Value for the Day Windows updates will install. Sunday = 1, Monday = 2,
    Tuesday =3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7'),
    [string] $UpdateTime = (Read-Host -Prompt 'Enter update time. Values use the numner to represent hour on a 24 Hour clock,
    i.e (03:00 =3) (18:00 = 18)')
    )

    And below is the output i get rather than including the output to user,

    cmdlet at command pipeline position 1
    Supply values for the following parameters:
    UpdateDay:

    Thanks in advance Leon.

  • #90649

    Jeremy Corbello
    Participant

    Use "HelpMessage" when declaring your parameters:

    function test {
        [CmdletBinding()]
        param(
        [Parameter( Mandatory=$True,
                    HelpMessage="Enter Value for the day Windows updates will install. Sunday = 1, Monday = 2, Tuesday =3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7"
                    )][String] $UpdateDay,
        [Parameter( Mandatory=$True,
                    HelpMessage="Enter update time. Values use the number to represent hour on a 24 Hour clock, i.e (03:00 =3) (18:00 = 18)"
                    )][string] $UpdateTime
        )
    }

    I'd also mention that the way you're asking users to enter data is inefficient and will most likely end up in errors. For the days, I'd use a set so they just select the day that they want. You can convert that to numerical form later in the script.
    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1#validateset-attribute
    The hour representation is fine, I suppose.

  • #90650

    Don Jones
    Keymaster

    Once you specify Mandatory, the command won't run unless a value is explicitly entered. Default values are ignored – including your Read trick.

  • #90656

    Leon P
    Participant

    Hello All,

    Thank you for the help, i have changed the script block to include a help message and convert the input to numeric later on. On a separate note how do you add the power-shell box Jeremy used in his post, this will help make my next post more clear?

  • #90682

    Leon P
    Participant

    The Script is now working :). However, the script i was writing was to automate the local group policy settings i generally use on server builds, such as setting WSUS server location. The script was using the Set-ItemProperty commands to edit the regisrty, now that the script runs i have noticed that gpupdate or logon/logoff overwrites the values that i have set, is there a way to stop this or a better way of doing this?

    I cannot add these to a domain as they are tailored for each clients needs and most do not want a domain controller. I have googled this but all solutions are generally installing third party software etc, which would take just as much time to do each time as just editing the local GP.

    • #90686

      Jeremy Corbello
      Participant

      So these are workgroup computers? i.e. not part of a domain environment?
      If that's the case, just add that script to startup on the PC's.

      Also, what is the reason for not having domained computers? Just curious on that one.

    • #90692

      Leon P
      Participant

      Hello Jeremy,

      Thanks for the advice, they are client servers that are bespoke for hosted applications etc. Naturally most clients are not willing to add the cost of a domain controller to manage the 1-2 servers they have with us.

  • #90695

    Jeremy Corbello
    Participant

    Ah, makes total sense now. Yeah, a startup script should do the trick. Or you could look into PowerShell DSC. Could give you more granularity and easier to manage in the long run.

    https://docs.microsoft.com/en-us/powershell/dsc/overview

  • #90766

    Leon P
    Participant

    Thanks Jeremy, i will have a look into DSC now 🙂

You must be logged in to reply to this topic.