Powershell : Ouvrir l'explorateur de fichier pour sélectionner un fichier

Résolu/Fermé
__Juulien__ Messages postés 20 Date d'inscription vendredi 11 mai 2018 Statut Membre Dernière intervention 14 novembre 2020 - Modifié le 16 avril 2020 à 08:24
yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 - 16 avril 2020 à 09:17
Bonjour,

J'aimerais lorsque l'on exécute mon script, mon programme demande à l'utilisateur de choisir un fichier pour la variable $file. En m'aidant d'internet j'ai trouvé le code ci-dessous mais je n'arrive pas à le faire fonctionner. Je l’exécute comme il est écris ci-dessous :



$file = Select-FileDialog -Title "Select a file"

function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="All Files (*.*)|*.*")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Write-Error "Operation cancelled by user."
}
}
A voir également:

2 réponses

yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 1 476
16 avril 2020 à 08:29
bonjour, " je n'arrive pas à le faire fonctionner": que se passe-t'il?
as-tu essayé en mettant la première ligne à la fin?
1
__Juulien__ Messages postés 20 Date d'inscription vendredi 11 mai 2018 Statut Membre Dernière intervention 14 novembre 2020
16 avril 2020 à 08:35
Oui le problème est probablement la première ligne. Si je ne la met pas, aucune erreur et si je la met plutôt à la fin cela ne change à rien.

Voici mes erreurs quand j'exécute le toute :

Cannot invoke method. Method invocation is supported only on core types in this language mode.

At C:\Users\FroidevauxJ\Documents\test.ps1:5 char:2
+ [System.Reflection.Assembly]::LoadWithPartialName("System.Windows ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage

New-Object : Cannot create type. Only core types are supported in this language mode.
At C:\Users\FroidevauxJ\Documents\test.ps1:6 char:13
+ $objForm = New-Object System.Windows.Forms.OpenFileDialog
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [New-Object], PSNotSupportedException
+ FullyQualifiedErrorId : CannotCreateTypeConstrainedLanguage,Microsoft.PowerShell.Commands.NewObjectCommand

The property 'InitialDirectory' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:7 char:2
+ $objForm.InitialDirectory = $Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property 'Filter' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:8 char:2
+ $objForm.Filter = $Filter
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property 'Title' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\FroidevauxJ\Documents\test.ps1:9 char:2
+ $objForm.Title = $Title
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Users\FroidevauxJ\Documents\test.ps1:10 char:2
+ $Show = $objForm.ShowDialog()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Select-FileDialog : Operation cancelled by user.
At line:1 char:9
+ $file = Select-FileDialog -Title "Select a file"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Select-FileDialog</code>
0
yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 1 476
16 avril 2020 à 08:49
et en exécutant ton script ainsi, dans une boite de commande?
PowerShell.exe -f c:\yourscript.ps1
0
__Juulien__ Messages postés 20 Date d'inscription vendredi 11 mai 2018 Statut Membre Dernière intervention 14 novembre 2020 > yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024
16 avril 2020 à 08:59
Ok d'accord je viens de comprendre c'est une question de droit, si je fais votre commande cela me renvoies les mêmes erreurs. Il suffit simplement d'ouvrir powershell en tant qu'administrateur.

Merci pour votre aide
0
yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 1 476 > __Juulien__ Messages postés 20 Date d'inscription vendredi 11 mai 2018 Statut Membre Dernière intervention 14 novembre 2020
16 avril 2020 à 09:17
parfait, peux-tu alors marquer le sujet comme résolu?
0