从命令提示符运行 PowerShell 命令(无 ps1 脚本) [英] Run PowerShell command from command prompt (no ps1 script)

查看:31
本文介绍了从命令提示符运行 PowerShell 命令(无 ps1 脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从命令提示符只运行几个 PowerShell 命令的方法.我不想为此创建脚本,因为它只是我需要运行的几个命令,而且我真的不知道如何使用 PowerShell 编写脚本.

I'm looking for a way to run just a couple PowerShell commands from the command prompt. I don't want to create a script for this since it's just a couple commands I need to run and since I don't really know how to script with PowerShell.

这是我尝试使用的命令:

Here is the command I'm trying to use to start with:

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

我真的不想为此创建脚本,因为如果我可以从批处理文件中运行一两个命令以及其余内容,那会容易得多.

I don't really want to create a script for this as it would be much easier if I can just run one or two commands from a batch file with the rest of the stuff.

这是我迄今为止尝试过的.

Here is what I've tried so far.

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.....}

这种方式没有错误,但我没有得到任何回报.如果我使用 Set-AppLockerPolicy... 什么也不会发生.

No error with this way but I don't get anything back. If I use the Set-AppLockerPolicy... nothing happens.

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....}

没有错误但没有任何反应.

No error but nothing happens.

7)

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

没有错误但没有任何反应.

No error but nothing happens.

推荐答案

这是成功解决我的问题的唯一答案,在 这个网页(很好的参考).

Here is the only answer that managed to work for my problem, got it figured out with the help of this webpage (nice reference).

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

此外,这是执行多个命令的巧妙方法:

Also, here is a neat way to do multiple commands:

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

例如,这就是我运行 2 个命令的方式:

For example, this is how I ran my 2 commands:

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

这篇关于从命令提示符运行 PowerShell 命令(无 ps1 脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆