Author Posts

January 2, 2018 at 5:07 pm

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.

January 2, 2018 at 5:29 pm

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.

January 2, 2018 at 5:31 pm

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

January 3, 2018 at 10:33 am

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?

January 3, 2018 at 11:13 am

How to Format Code in the Forums (That's the first post on top of the list in this forum!)

January 3, 2018 at 3:53 pm

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.

January 3, 2018 at 4:12 pm

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.

January 3, 2018 at 4:49 pm

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.

January 3, 2018 at 5:24 pm

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

January 4, 2018 at 10:09 am

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