JFLarvoire / SysToolsLib

A library of Windows and Linux system management tools
Apache License 2.0
314 stars 93 forks source link

[PSService] Single -setup parameter not working for powershell v2 #22

Open cloverhap opened 5 years ago

cloverhap commented 5 years ago

I was testing the (mostly) unmodified service script on a machine with powershell v2 installed as we have some in our environment, and came across this message:

Parameter set cannog be resolved using the specified named parameters. At line:1 char:32

I removed line 233 (below) to make it work since I won't be using the 'Setup' parameter set, but i'm wondering if this is happening on other users as well?

[Parameter(ParameterSetName='Setup', Mandatory=$true)] <<< [Parameter(ParameterSetName='Setup2', Mandatory=$true)] [Switch]$Setup, # Install the service

cbm-afettach commented 4 years ago

Hello @cloverhap. I had the same issue and commenting the line 233 resolved it. Thanks

JFLarvoire commented 4 years ago

Hello,

What exact OS and PS versions do you have? Because on the only system I have with PS v2, a Windows 2008 R2 server, it works just fine:

PS C:\Users\Administrator> $psversiontable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.8806
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

PS C:\Users\Administrator> psservice.ps1 -?

NAME
C:\Tools\PSService.ps1

SYNOPSIS
A sample Windows service, in a standalone PowerShell script.

SYNTAX
C:\Tools\PSService.ps1 [-Status] [<CommonParameters>]

C:\Tools\PSService.ps1 -Start [<CommonParameters>]

C:\Tools\PSService.ps1 -Stop [<CommonParameters>]

C:\Tools\PSService.ps1 -Restart [<CommonParameters>]

C:\Tools\PSService.ps1 -Setup [-Credential <PSCredential>] [<CommonParameters>]

C:\Tools\PSService.ps1 -Setup -UserName <String> [-Password <String>] [<CommonParameters>]

C:\Tools\PSService.ps1 -Remove [<CommonParameters>]

C:\Tools\PSService.ps1 -Service [<CommonParameters>]

C:\Tools\PSService.ps1 -SCMStart [<CommonParameters>]

C:\Tools\PSService.ps1 -SCMStop [<CommonParameters>]

C:\Tools\PSService.ps1 -Control <String> [<CommonParameters>]

C:\Tools\PSService.ps1 -Version [<CommonParameters>]

DESCRIPTION
This script demonstrates how to write a Windows service in pure PowerShell.
It dynamically generates a small PSService.exe wrapper, that in turn
invokes this PowerShell script again for its start and stop events.

RELATED LINKS

REMARKS
To see the examples, type: "get-help C:\Tools\PSService.ps1 -examples".
For more information, type: "get-help C:\Tools\PSService.ps1 -detailed".
For technical information, type: "get-help C:\Tools\PSService.ps1 -full".

PS C:\Users\Administrator>
JFLarvoire commented 2 weeks ago

Actually there's a simple workaround. Run: PSService.ps1 -Setup -UserName LocalSystem This satisfies PowerShell 2, and does not change the User Name as LocalSystem is the default user name that would be used anyway.