IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

ouvrir Powershell en mode administrateur dans le dossier parent avec le menu contextuel [PowerShell]


Sujet :

Scripts/Batch

  1. #1
    Membre averti
    Homme Profil pro
    Monteur Vidéo...
    Inscrit en
    Avril 2019
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Monteur Vidéo...

    Informations forums :
    Inscription : Avril 2019
    Messages : 29
    Par défaut ouvrir Powershell en mode administrateur dans le dossier parent avec le menu contextuel
    Salut,

    J'ai ce problème avec ça :
    Pour ouvrir Powershell en mode administrateur dans le dossier parent avec le menu contextuel


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
    @="Ouvrir Windows PowerShell (Admin)"
    "Extended"=-
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
    @="Powershell.exe -noexit -Command Set-Location -literalPath '%V' \"& powershell Start-Process powershell -Verb runAs"

    J'ai cette erreur quand je clique droit et que je fait "Ouvrir Windows PowerShell (Admin)"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Au caractère Ligne:1 : 50
    + Set-Location -literalPath 'C:\Users\Nom d'utilisateur\Desktop' & powershell Start-P ...
    +                                                  ~
    Le caractère perluète (&) n’est pas autorisé. L’opérateur & est réservé à une utilisation future. Placez un caractère
    perluète entre guillemets doubles ("&") pour que ce symbole soit considéré comme une chaîne.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : AmpersandNotAllowed

    Celui là fonctionne, mais pas en mode administrateur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
    @="Ouvrir Windows PowerShell (Admin)"
    "Extended"=-
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CLASSES_ROOT\Directory\shell\Powershell\command]
    @="powershell.exe -noexit -command Set-Location -literalPath '%V'"

    Si quelqu'un à une idée

    Merci

    @+

  2. #2
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Essai de remplacer le & par un ;

  3. #3
    Membre averti
    Homme Profil pro
    Monteur Vidéo...
    Inscrit en
    Avril 2019
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Monteur Vidéo...

    Informations forums :
    Inscription : Avril 2019
    Messages : 29
    Par défaut
    Citation Envoyé par ericlm128 Voir le message
    Essai de remplacer le & par un ;
    Salut,

    Merci, ça fonctionne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
    @="Ouvrir Windows PowerShell (Admin)"
    "Extended"=-
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
    @="Powershell.exe -Command Set-Location -literalPath '%V' \"; powershell Start-Process powershell -Verb runAs"
    Par contre, j'ai cette erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Le terminateur ' est manquant dans la chaîne.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
    
    PS J:\+ FICHIERS BAT\Ajouter dans le Menu Contextuel de l'explorateur windows\Ouvrir ICI - PowerShell admin au menu contextuel_Windows 10>
    Quand je suis dans un dossier où le chemin à un nom long…

    Merci

    @+

  4. #4
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    C'est du au fait que ton chemin comporte un '
    J'ai le même soucis avec le raccourci natif Microsoft sans runAs (en laissant shift appuyé lors du click droit sur un dossier.
    Ton code à un défaut, l'élévation de privilège redéfinit toujours le dossier de travail dans system32. Il faut donc le positionné après et pas avant.

    La syntaxe semble difficile à trouver entre les multiples échappements nécessaires

  5. #5
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 218
    Par défaut
    Je te propose ceci

    C:\RunAsInFolder.ps1
    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    param
    (
        [Parameter(Mandatory)]
        [System.IO.FileInfo]$path
    )
    $verbatim = @'
    Start-Process -FilePath Powershell -Verb runAs -ArgumentList "-noexit -Command `"Set-Location -literalPath \""$PATH\""`""
    '@
    $verbatim = $verbatim.Replace("`$PATH", $path)
    $encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($verbatim))
    Start-Process -FilePath Powershell -NoNewWindow -ArgumentList "-EncodedCommand $encodedCommand"

    Registre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    @="powershell.exe -File "C:\RunAsInFolder.ps1" "%V""
    Je n'ai juste pas testé la partie registre

  6. #6
    Membre averti
    Homme Profil pro
    Monteur Vidéo...
    Inscrit en
    Avril 2019
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Monteur Vidéo...

    Informations forums :
    Inscription : Avril 2019
    Messages : 29
    Par défaut
    Salut,

    Merci, ça fonctionne nickel...

    RunAsInFolder.ps1
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    param
    (
        [Parameter(Mandatory)]
        [System.IO.FileInfo]$path
    )
    $verbatim = @'
    Start-Process -FilePath Powershell -Verb runAs -ArgumentList "-noexit -Command `"Set-Location -literalPath \""$PATH\""`""
    '@
    $verbatim = $verbatim.Replace("`$PATH", $path)
    $encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($verbatim))
    Start-Process -FilePath Powershell -NoNewWindow -ArgumentList "-EncodedCommand $encodedCommand"
    RunAsInFolder.reg
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
    @="Ouvrir Windows PowerShell (Admin)"
    "Extended"=-
    "HasLUAShield"=""
    "Icon"="powershell.exe"
    
    [HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
    @="powershell.exe -File \"C:\\RunAsInFolder.ps1\" \"%V\""
    OU

    Création du fichier "RunAsInFolder.ps1" et copie dans "C:\Users\Nom d'utilisateur"
    et Ajout à la base de registre


    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    @echo off
    @C:\Windows\System32\chcp 28591 > nul
    @C:\Windows\System32\mode con cols=101 lines=20
    @Title Exécution en Mode ADMIN 
    :: On execute les commandes en administateur
    ::------------------------------------------
    REM --> Verification des permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    REM --> Erreur vous ne possedez pas les droits admin
    if '%errorlevel%' NEQ '0' (
    REM --> Verification des privileges administrateur
    goto UACPrompt
    ) else ( goto gotAdmin )
    :UACPrompt
    @echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "%~s0", "%params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B
    :gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
    @cls
    ::------------------------------------------
    :: Création du fichier "RunAsInFolder.ps1" et copie dans "C:\Users\Nom d'utilisateur\"
    echo param >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo ( >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo    [Parameter(Mandatory)] >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo    [System.IO.FileInfo]$path >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo ) >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo $verbatim = @' >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo Start-Process -FilePath Powershell -Verb runAs -ArgumentList "-noexit -Command `"Set-Location -literalPath \""$PATH\""`"" >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo '@ >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo $verbatim = $verbatim.Replace("`$PATH", $path) >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo $encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($verbatim)) >> "%USERPROFILE%\RunAsInFolder.ps1"
    echo Start-Process -FilePath Powershell -NoNewWindow -ArgumentList "-EncodedCommand $encodedCommand" >> "%USERPROFILE%\RunAsInFolder.ps1"
    ::------------------------------------------
    :: Ajout à la base de registre
    @ping 127.0.0.1 -n 2 > NUL
    reg delete "HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin" /v "Extended" /f >nul 2>&1
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin" /v "" /d "Ouvrir Windows PowerShell (Admin)" /f > NUL
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin" /v "HasLUAShield" /d "" /f > NUL
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin" /v "Icon" /d "powershell.exe" /f > NUL
    reg add "HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command" /v "" /d "powershell.exe -File \"%USERPROFILE%\\RunAsInFolder.ps1\" \"%%V\"" /f > NUL
    @ping 127.0.0.1 -n 2 > NUL
    @exit

    Encore merci

    @+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [PowerShell] Powershell (executer des .exe dans un dossier)
    Par Fallens dans le forum Scripts/Batch
    Réponses: 0
    Dernier message: 22/06/2021, 11h10
  2. Réponses: 3
    Dernier message: 07/11/2008, 11h41
  3. ouvrir un PDF et enregistrer dans un dossier
    Par ericdev67 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 07/03/2008, 19h32
  4. effacer un fichier dans un dossier parent
    Par pierrot10 dans le forum Langage
    Réponses: 1
    Dernier message: 13/06/2007, 22h28
  5. acces à la Bd situee dans le dossier parent
    Par trialrofr dans le forum ASP
    Réponses: 4
    Dernier message: 04/01/2005, 17h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo