Bonjour,
Je suis nouveau dans le powershell.
J'aimerais construire une fonction avec 2 paramètres obligatoires et 1 paramètre qu'il n'est pas obligatoire de saisir.
Champs obligatoires :
- varDriveName
- varMappingPath
Champ pas obligatoires :
- Persist
J'ai trouvé un contournement mais c'est pas vraiment propre car a chaque fois que j'appel la fonction, je dois tagger le "y" ou le "n" pour savoir si le lecteur va être persistant ou non.
Appel de la fonction pour un lecteur persistant : "MountNetworkDrive u \\xxx\mypath y"
Appel de la fonction pour un lecteur non persistant : "MountNetworkDrive u \\xxx\mypath n"
L'idée, ca serait de pouvoir enlever le paramètre quand j'ai pas besoin d'un lecteur persistant et de mettre -persist quand j'ai besoin d'un lecteur persistant et si possible dans le code d'enlever le IF qui test si le paramètre persist égal "y" ou "n" car c'est pas très beau.
Exemple de l'appel:
Appel de la fonction pour un lecteur persistant : "MountNetworkDrive u \\xxx\mypath -persist"
Appel de la fonction pour un lecteur non persistant : "MountNetworkDrive u \\xxx\mypath"
Exemple du code de la fonction:
New-PSDrive $DriveName -PSProvider FileSystem -Root $varMappingPath $Persist
Merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 function CreateNetworkDrive{ <# .SYNOPSIS Function who create a network drive for a specific repository. .FUNCTIONALITY Network Drive .DESCRIPTION Function who create a network drive for a specific repository .PARAMETER varDriveName Name of the network drive you want to mount .PARAMETER varMappingPath Full path to the directory you want to mount .PARAMETER Persist Fill 'y' or 'n' if you want a persistent network drive or not #> PARAM ( [string] $varDriveName, [string] $varMappingPath, [string] $Persist ) if($Persist -eq "y"){ New-PSDrive $DriveName -PSProvider FileSystem -Root $varMappingPath -Persist } elseif ($Persist -eq "n"){ New-PSDrive $DriveName -PSProvider FileSystem -Root $varMappingPath } else{ DisplayLog "Operation failed" } }
Partager