Parameter Set Names

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 9 months, 2 weeks ago.

  • Author
    Posts
  • #35619
    Profile photo of Nigel Tatschner
    Nigel Tatschner
    Participant

    Hi All, I'm having issues with my code, I'm specifying parameter set names but they don't seem to be working anymore.

     Get-NTService -ComputerName MYSERVER -StartMode Auto
    	
    	.OUTPUTS
    		System.String, System.String
    	
    	.NOTES
    		Additional information about the function.
    #>
    function Get-NTService
    {
    	[CmdletBinding(DefaultParameterSetName = 'Default')]
    	[OutputType([System.String], ParameterSetName = 'Default')]
    	[OutputType([System.String], ParameterSetName = 'Service')]
    	[OutputType([System.String], ParameterSetName = 'Summary')]
    	[OutputType([System.String], ParameterSetName = 'StartMode')]
    	[OutputType([System.Object])]
    	param
    	(
    		[Parameter(ParameterSetName = 'Default',
    				   Mandatory = $false,
    				   ValueFromPipeline = $true,
    				   ValueFromPipelineByPropertyName = $true)]
    		[Parameter(ParameterSetName = 'Service')]
    		[Parameter(ParameterSetName = 'Summary')]
    		[Parameter(ParameterSetName = 'StartMode')]
    		[Alias('Server')]
    		[System.String]$ComputerName = 'localhost',
    		[Parameter(ParameterSetName = 'Default')]
    		[Parameter(ParameterSetName = 'StartMode')]
    		[Parameter(ParameterSetName = 'Service')]
    		[ValidateSet('Auto', 'AutoNotRunning', 'Manual', 'All', IgnoreCase = $true)]
    		[System.String]$StartMode = 'All',
    		[Parameter(ParameterSetName = 'Summary')]
    		[switch]$Summary,
    		[Parameter(ParameterSetName = 'Service')]
    		[Parameter(ParameterSetName = 'StartMode')]
    		[Parameter(ParameterSetName = 'Default')]
    		[System.String]$ServiceName,
    		[Parameter(ParameterSetName = 'Service',
    				   Mandatory = $false)]
    		[Parameter(ParameterSetName = 'Default')]
    		[Parameter(ParameterSetName = 'StartMode')]
    		[ValidateSet('Start', 'Stop', 'Restart', 'Pause', 'Resume', 'Interrogate')]
    		[System.String]$Action = 'Interrogate',
    		[Parameter(ParameterSetName = 'StartMode')]
    		[Parameter(ParameterSetName = 'Service')]
    		[Parameter(ParameterSetName = 'Default')]
    		[ValidateSet('Automatic', 'Manual', 'Disabled')]
    		[System.String]$AlterStartMode
    	)
    	
    	Begin
    	{
    		
    	}
    	Process
    	{
    		# Start Process Block
    		switch ($PsCmdlet.ParameterSetName)
    		{
    			"Default" {
    				
    				Try
    				{
    					Write-Verbose "Attempting to gather Service(s) from $($ComputerName)"
    					$Services = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    					
    					Write-Verbose "Gather Service(s) from $($ComputerName)"
    				}
    				Catch [System.UnauthorizedAccessException]
    				{
    					Write-Verbose "Access Denied - Could not connect to $($ComputerName) via WMI"
    					Write-Error -Message 'Access Denied - Could not connect via WMI'
    					
    				}
    				Catch [Exception]
    				{
    					Switch -regex ($Error[0])
    					{
    						"The RPC server is unavailable" { Write-Verbose "Connection Issue - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Connection Issue - Could not connect via WMI' }
    						Default { Write-Verbose "Unknown Error - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Unknown Error - Could not connect via WMI' }
    					}
    				}
    				Write-Verbose "Building Output Object"
    				foreach ($a in $Services)
    				{
    					$Obj = New-Object -TypeName System.Management.Automation.PSObject
    					$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $a.PSComputerName
    					$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $a.Name
    					$Obj | Add-Member -MemberType NoteProperty -Name "ServiceDescription" -Value $a.Description
    					$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $a.Status
    					$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $a.State
    					$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $a.StartMode
    					
    					if ($StartMode -eq "All")
    					{
    						Write-Output $Obj
    					}
    					elseif ($StartMode -eq "Auto")
    					{
    						$Obj = $Obj | Where-Object -Property StartMode -EQ "Auto"
    						Write-Output $Obj
    					}
    					elseif ($StartMode -eq "AutoNotRunning")
    					{
    						$Obj = $Obj | Where-Object { ($_.StartMode -EQ "Auto") -and ($_.State -notlike "Running") }
    						Write-Output $Obj
    					}
    					elseif ($StartMode -eq "Manual")
    					{
    						$Obj = $Obj | Where-Object -Property StartMode -EQ "Manual"
    						Write-Output $Obj
    					}
    				}
    			}
    			"Service" {
    				Try
    				{
    					Write-Verbose "Attempting to verify Service name from $($ComputerName)"
    					$Services = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    					$Services = $Services | Where-Object -Property Name -like $ServiceName
    					
    					switch ($Action)
    					{
    						"Start" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.StartService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						"Stop" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.StopService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						"Restart" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.StopService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    								Write-Output "Restarting Service $ServiceName"
    								$Action = $b.StartService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    							
    						}
    						"Pause" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.PauseService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    							
    						}
    						"Resume" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.ResumeService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						"Interrogate" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.InterrogateService() | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "The error value $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Result"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						Default { }
    					}
    				}
    				Catch [System.UnauthorizedAccessException]
    				{
    					Write-Verbose "Access Denied - Could not connect to $($ComputerName) via WMI"
    					Write-Error -Message 'Access Denied - Could not connect via WMI'
    					$Result = "Failed"
    				}
    				Catch [Exception]
    				{
    					Switch -regex ($Error[0])
    					{
    						"The RPC server is unavailable" { Write-Verbose "Connection Issue - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Connection Issue - Could not connect via WMI' }
    						Default { Write-Verbose "Unknown Error - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Unknown Error - Could not connect via WMI' }
    					}
    					$Result = "Failed"
    				}
    			}
    			"Summary" {
    				
    				Try
    				{
    					Write-Verbose "Attempting to gather Service(s) from $($ComputerName)"
    					$Services = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    					
    					Write-Verbose "Gather Service(s) from $($ComputerName)"
    				}
    				Catch [System.UnauthorizedAccessException]
    				{
    					Write-Verbose "Access Denied - Could not connect to $($ComputerName) via WMI"
    					Write-Error -Message 'Access Denied - Could not connect via WMI'
    					
    				}
    				Catch [Exception]
    				{
    					Switch -regex ($Error[0])
    					{
    						"The RPC server is unavailable" { Write-Verbose "Connection Issue - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Connection Issue - Could not connect via WMI' }
    						Default { Write-Verbose "Unknown Error - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Unknown Error - Could not connect via WMI' }
    					}
    				}
    				$Obj = New-Object -TypeName System.Management.Automation.PSObject
    				$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $Services[0].PSComputerName
    				$Obj | Add-Member -MemberType NoteProperty -Name "TotalServicesCount" -Value $Services.Count
    				$Obj | Add-Member -MemberType NoteProperty -Name "AutoStartCount" -Value ($Services | Where-Object -Property StartMode -EQ Auto).count
    				$Obj | Add-Member -MemberType NoteProperty -Name "AutoStartButNotRunningCount" -Value ($Services | Where-Object { ($_.StartMode -EQ "Auto") -and ($_.State -notlike "Running") }).count
    				
    				Write-Output $Obj
    			}
    			"StartMode" {
    				Try
    				{
    					Write-Verbose "Attempting to verify Service name from $($ComputerName)"
    					$Services = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    					$Services = $Services | Where-Object -Property Name -like $ServiceName
    					
    					switch ($AlterStartMode)
    					{
    						"Automatic" {
    							foreach ($b in $Services)
    							{
    								$Action = $b.ChangeStartMode("$AlterStartMode") | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "Error $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Results"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						"Disabled" {
    							foreach ($b in $Services)
    							{
    								$b.ChangeStartMode("$AlterStartMode") | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "Error $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Results"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						"Manual" {
    							foreach ($b in $Services)
    							{
    								$b.ChangeStartMode("$AlterStartMode") | Out-Null
    								$Service = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName -ErrorAction Stop
    								$Results = (if $Action.returnvalue -eq 0 { "Succsess" }	else { "Error $($Action.returnvalue) was reported" })
    								$b = $Service | Where-Object -Property Name -EQ $b.Name
    								Write-Verbose "Verified service name from $($ComputerName)"
    								$Obj = New-Object -TypeName System.Management.Automation.PSObject
    								$Obj | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $b.PSComputerName
    								$Obj | Add-Member -MemberType NoteProperty -Name "ServiceName" -Value $b.Name
    								$Obj | Add-Member -MemberType NoteProperty -Name "Action" -Value "$Action"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Result" -Value "$Results"
    								$Obj | Add-Member -MemberType NoteProperty -Name "Status" -Value $b.Status
    								$Obj | Add-Member -MemberType NoteProperty -Name "State" -Value $b.State
    								$Obj | Add-Member -MemberType NoteProperty -Name "StartMode" -Value $b.StartMode
    								Write-Output $Obj
    							}
    						}
    						Default { }
    					}
    				}
    				Catch [System.UnauthorizedAccessException]
    				{
    					Write-Verbose "Access Denied - Could not connect to $($ComputerName) via WMI"
    					Write-Error -Message 'Access Denied - Could not connect via WMI'
    					$Result = "Failed"
    				}
    				Catch [Exception]
    				{
    					Switch -regex ($Error[0])
    					{
    						"The RPC server is unavailable" { Write-Verbose "Connection Issue - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Connection Issue - Could not connect via WMI' }
    						Default { Write-Verbose "Unknown Error - Could not connect to $($ComputerName) via WMI"; Write-Error -Message 'Unknown Error - Could not connect via WMI' }
    					}
    					$Result = "Failed"
    				}
    			}
    		}
    	} # End Process Block
    	End
    	{
    		
    	}
    }
    
  • #35621
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Looks okay to me. Can you be more specific about what you mean by "don't seem to be working anymore"?

  • #35626
    Profile photo of Nigel Tatschner
    Nigel Tatschner
    Participant

    It seems to be ignoring the set and just using default

  • #35664
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    All parameter sets excluding Summary have the same parameters. what the catch ?
    How PS should determine what parameter set to use?

  • #35680
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Heh, I didn't even notice that. 🙂

You must be logged in to reply to this topic.