Parameter "SqlServer" is declared in parameter-set "__AllParameterSets" multi

Welcome Forums General PowerShell Q&A Parameter "SqlServer" is declared in parameter-set "__AllParameterSets" multi

This topic contains 1 reply, has 1 voice, and was last updated by

 
Participant
3 weeks ago.

  • Author
    Posts
  • #125667

    Participant
    Points: 123
    Rank: Participant

    I want to be able to pass multiple parameters to the function but keep getting an error that I can't see why I'm getting it.

    The parameter "SqlServer" is declared in parameter-set "__AllParameterSets" multiple times.
    
    

    I'm using this Command:

    @([pscustomobject]@{
    SqlServer = "hostserver\sqlinstance"
    Database = "databasename"
    }) | Recover-LogSpace
    
    

     

    To call this function:

    Function Recover-LogSpace
    {
    [CmdletBinding(DefaultParameterSetName="Multiple")]
    Param(
    [parameter(Mandatory=$true,Position=0)]
    [Parameter(ParameterSetName='Multiple')]
    [parameter(ParameterSetName='Single')]
    [parameter(ValuefromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
    [string[]]$SqlServer,
    [parameter(Mandatory=$false,Position=1)]
    [parameter(ParameterSetName='Single')]
    [parameter(ValuefromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
    [string[]]$Database
    )
    
    BEGIN {
    # https://docs.microsoft.com/en-us/dotnet/api/microsoft.sqlserver.management.smo.shrinkmethod?view=sqlserver-2016
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
    }
    PROCESS {
    foreach($server in $SqlServer) {
    $srv = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
    if($Database) {
    $srv.Databases[$Database] | ?{ $_.Shrink(2,[Microsoft.SqlServer.Management.Smo.ShrinkMethod]'TruncateOnly') }
    }
    else {
    $srv.Databases | ?{ $_.ID -gt "4" -And $_.IsAccessible -eq $True -And $_.ReadOnly -eq $False } | ?{ $_.Shrink(2,[Microsoft.SqlServer.Management.Smo.ShrinkMethod]'TruncateOnly') }
    }
    }
    }
    END {
    }
    }
    
    
  • #125679

    Participant
    Points: 123
    Rank: Participant

    I finally figure it out....the problem was this:

    
    [Parameter(ParameterSetName='Multiple')]
    [parameter(ParameterSetName='Single')]
    
    

You must be logged in to reply to this topic.