Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Funktion Parameter Switch (Schalter) - ParameterBindingException (mehrere Schalter nutzen)

$
0
0

Hallo Zusammen,

hoffe wieder hier Unterstützung zu finden ...

Umfeld:

+ PowerShell 4

Aufgabenbeschreibung:
Mittels einer Funktion (hier ein gezipptes Archiv erstellen) möchte ich mit den "Switch"-Schalter-HideWindow und -OpenWithWindowsExplorer 2 Optionen einbauen, die entweder aktiviert oder eben im Standard nicht aktiviert werden.

CODE:

Function Write7ZipEncryptedAes7zFile {
#
[cmdletbinding()]
PARAM (

    [Parameter(Mandatory=$True,
        Position=0,
        HelpMessage="ENTER: File path to files for ZIP like 'C:\Source\PS\Source\'")]
        [ValidateNotNullOrEmpty()]
        [System.String] $FilesToZip,

    [Parameter(Mandatory=$True,
        Position=1,
        HelpMessage="ENTER: Output File Path like 'C:\Source\PS\Target\'")]
        [ValidateNotNullOrEmpty()]
        [System.String] $OutputFilePath,

    [Parameter(Mandatory=$True,
        Position=2,
        HelpMessage="ENTER: Output File Name like 'Output.7z'")]
        [ValidateNotNullOrEmpty()]
        [System.String] $OutputFileName,

    [Parameter(Mandatory=$True,
        Position=3,
        HelpMessage="ENTER: File Compression Type like '7z'")]
        [ValidateSet('7z')]
        [System.String] $CompressionType,

    [Parameter(Mandatory=$True,
        Position=4,
        HelpMessage="ENTER: Zip File password like 'p@ssw0rd'")]
        [ValidateNotNullOrEmpty()]
        [System.String] $Password,

    [Parameter(Mandatory=$False,
        Position=5,
        ParameterSetName="HideWindow",
        HelpMessage="OPTION: 'Set 7Zip Windows Style to hidden")]
        [Switch] $HideWindow,

    [Parameter(Mandatory=$False,
        Position=6,
        ParameterSetName="OpenWithWindowsExplorer",
        HelpMessage="OPTION: 'Open Zip Output File Path with Windows Explorer")]
        [Switch] $OpenWithWindowsExplorer

)                                         # PARAMETER BLOCK
#
Begin {
    #
    # TEST 7zip executable.
    $PathTo32Bit7Zip         = "C:\Program Files (x86)\7-Zip\7z.exe"
    $PathTo64Bit7Zip         = "C:\Program Files\7-Zip\7z.exe"
    # $THIS_SCRIPTS_DIRECTORY  = Split-Path $script:MyInvocation.MyCommand.Path
    # $pathToStandAloneExe     = Join-Path $THIS_SCRIPTS_DIRECTORY "7za.exe"
    #
    Write-Host "TEST`t:`t`t 7Zip executable" -ForegroundColor Yellow -NoNewline
    IF (Test-Path $PathTo64Bit7Zip) { $PathTo7ZipExe = $PathTo64Bit7Zip; Write-Host -ForegroundColor Green "`t`t`t OK`t 64Bit" }
        ELSEIF (Test-Path $PathTo32Bit7Zip) { $PathTo7ZipExe = $PathTo32Bit7Zip; Write-Host -ForegroundColor Green "`t`t`t OK`t 32Bit" }
        ELSEIF (Test-Path $PathToStandAloneExe) { $PathTo7ZipExe = $PathToStandAloneExe; Write-Host -ForegroundColor Green "`t`t`t OK`t StandAlone" }
        ELSE { Write-Warning "EXIT: Could not find the 7-zip executable", Sleep 5, Exit 0 }
    #
    $OutputFile = $OutputFilePath + $OutputFileName
    #
    # TEST Target Directory
    Write-Host "TEST`t:`t`t Output File Path" -ForegroundColor Yellow -NoNewline
    IF (!(Test-Path $OutputFilePath)) { Write-Host -ForegroundColor Red "`t`t`t ERROR"; Write-Warning -Message 'EXIT: Could not find Target directory'; Sleep 3; Exit 0 }
    ELSE { Write-Host -ForegroundColor Green "`t`t`t OK" }
    #
    # Delete the destination zip file if it already exists (i.e. overwrite it)
    Write-Host "REMOVE`t:`t`t Output File" -ForegroundColor Yellow -NoNewline
    IF (Test-Path $OutputFile) { Remove-Item $OutputFile -Force; Write-Host -ForegroundColor Green "`t`t`t`t OK" }
    ELSE { Write-Host -ForegroundColor Green "`t`t`t`t OK`t NotFound" }
    #
    $WindowStyle = "Normal"
    IF ($HideWindow) { $windowStyle = "Hidden" }
    #
    }
#
Process {
    #
    # Create the arguments to use to zip up the files
    $Arguments = "a -t$CompressionType ""$OutputFile"" ""$FilesToZip"" -mhe"
    IF (!([string]::IsNullOrEmpty($Password))) { $Arguments += " -p$Password" }
    #
    # Zip up the files.
    Write-Host "WRITE`t:`t`t 7Zip AES256 File" -ForegroundColor Yellow -NoNewline
    $P = Start-Process $PathTo7ZipExe -ArgumentList $Arguments -Wait -PassThru -WindowStyle $WindowStyle
    #
    # If the files were not zipped successfully
    IF (!(($P.HasExited -eq $true) -and ($P.ExitCode -eq 0))) { Write-Host -ForegroundColor Red "`t`t`t ERROR"; Write-Warning  "There was a problem creating the zip file '$ZipFilePath'."; Sleep 3; Exit 0 }
    ELSE { Write-Host -ForegroundColor Green "`t`t`t OK" }
    #
    }
#
End {
    IF ( $OpenWithWindowsExplorer ) { explorer $OutputFilePath }
    }
#
} # FUNCTION BLOCK


Problemstellung:
Soweit sind die Schalter einsatzfähig, jedoch aber immer nur einer!? Es wird nach der Auswahl eines auch kein weiterer "Schalter" mehr angezeigt.

Führe ich die Schalter "erzwungen" hinter einander aus, so erhalte ich einen 'ParameterBindingException'

Write7ZipEncryptedAes7zFile -FilesToZip 'C:\Test\Source\' -OutputFilePath 'C:\Test\Target\' -OutputFileName 'Output.7z' -Password 'test' -CompressionType 7z -OpenWithWindowsExplorer -HideWindow

Write7ZipEncryptedAes7zFile : Der Parametersatz kann mit den angegebenen benannten Parametern nicht aufgelöst werden.
In Zeile:1 Zeichen:1
+ Write7ZipEncryptedAes7zFile -FilesToZip 'C:\Test\Source\' -OutputFilePath 'C:\Te ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : InvalidArgument: (:) [Write7ZipEncryptedAes7zFile], ParameterBindingException+ FullyQualifiedErrorId : AmbiguousParameterSet,Write7ZipEncryptedAes7zFile

Fragen:

Wie ist es möglich mehrere "Switch" Optionen in die Funktion einzubauen?

Vielen Dank im Voraus für jede Art der Zusammenarbeit.


Manfred Schüler


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>