web-dev-qa-db-fra.com

Exécuter la commande PowerShell à partir de l'invite de commande (pas de script ps1)

Je cherche un moyen d'exécuter quelques commandes PowerShell à partir de l'invite de commande. Je ne veux pas créer de script pour cela, car ce ne sont que quelques commandes que je dois exécuter et je ne sais pas vraiment comment écrire des scripts avec PowerShell.

Voici la commande que j'essaie d'utiliser pour commencer:

Get-AppLockerFileInformation -Directory <folderpath> -Recurse -FileType <type>

Je ne veux pas vraiment créer un script pour cela, car ce serait beaucoup plus facile si je pouvais juste exécuter une ou deux commandes à partir d'un fichier batch avec le reste.

EDIT: Voici ce que j'ai essayé jusqu'à présent.

1)

powershell -Command "Get-AppLockerFileInformation....."
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....

2)

powershell -Command {Get-AppLockerFileInformation.....}

Pas d'erreur avec cette façon mais je ne récupère rien. Si j'utilise le Set-AppLockerPolicy... Rien ne se passe.

3)

powershell -Command "{Get-AppLockerFileInformation.....}"
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....

4)

powershell -Command "& {Get-AppLockerFileInformation.....}"
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....

5)

powershell "& {Get-AppLockerFileInformation.....}"
Error: The term 'Get-AppLockerFileInformation is not recognized as the name of a cmdlet, function, script file, or operable program....

6)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command {Get-AppLockerFileInformation....}

Aucune erreur mais rien ne se passe.

7)

powershell -ExecutionPolicy Bypass -NoLogo -NoProfile -Command "Get-AppLockerFileInformation...."

Aucune erreur mais rien ne se passe.

30
Chef Pharaoh

Voici la seule réponse qui a réussi à résoudre mon problème, l’a trouvée avec l’aide de this page Web (Nice référence).

powershell -command "& {&'some-command' someParam}"

En outre, voici une manière soignée de faire plusieurs commandes:

powershell -command "& {&'some-command' someParam}"; "& {&'some-command' -SpecificArg someParam}"

Par exemple, voici comment j'ai exécuté mes 2 commandes:

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}"
52
Chef Pharaoh

Exécutez-le sur une seule ligne de commande comme ceci:

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile 
  -WindowStyle Hidden -Command "Get-AppLockerFileInformation -Directory <folderpath> 
  -Recurse -FileType <type>"
9
djangofan

Peut être powershell -Command "Get-AppLockerFileInformation....."

Jeter un coup d'œil à powershell /?

2
NiKiZe