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

Résolu
__Juulien__ Messages postés 20 Date d'inscription   Statut Membre Dernière intervention   -  
yg_be Messages postés 23541 Date d'inscription   Statut Contributeur Dernière intervention   -
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 23541 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 583
 
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   Statut Membre Dernière intervention  
 
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 23541 Date d'inscription   Statut Contributeur Dernière intervention   1 583
 
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   Statut Membre Dernière intervention   > yg_be Messages postés 23541 Date d'inscription   Statut Contributeur Dernière intervention  
 
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 23541 Date d'inscription   Statut Contributeur Dernière intervention   1 583 > __Juulien__ Messages postés 20 Date d'inscription   Statut Membre Dernière intervention  
 
parfait, peux-tu alors marquer le sujet comme résolu?
0