43

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

这是我试图用来开始的命令:

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

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

编辑:这是我到目前为止所尝试的。

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...什么都不会发生。

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

没有错误,但没有任何反应。

7)

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

没有错误,但没有任何反应。

4

4 回答 4

66

这是设法解决我的问题的唯一答案,在网页的帮助下弄清楚了(很好的参考)。

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

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

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

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

powershell -command "& {&'Import-Module' AppLocker}"; "& {&'Set-AppLockerPolicy' -XmlPolicy myXmlFilePath.xml}"
于 2015-01-13T00:05:21.990 回答
12

在单个命令行上运行它,如下所示:

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile 
  -WindowStyle Hidden -Command "Get-AppLockerFileInformation -Directory <folderpath> 
  -Recurse -FileType <type>"
于 2013-08-27T00:16:19.910 回答
2

也许powershell -Command "Get-AppLockerFileInformation....."

看一眼powershell /?

于 2013-08-26T23:31:22.813 回答
0

这适用于我的 Windows 10 的 cmd.exe 提示

powershell -ExecutionPolicy Bypass -Command "Import-Module C:\Users\william\ps1\TravelBook; Get-TravelBook Hawaii"

这个例子显示

  1. 如何链接多个命令
  2. 如何使用模块路径导入模块
  3. 如何运行模块中定义的函数
  4. 不需要那些花哨的“&”。
于 2020-07-22T17:33:42.783 回答