首页 文章

从命令提示符运行PowerShell命令(没有ps1脚本)

提问于
浏览
25

我正在寻找一种从命令提示符运行几个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...."

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

3 回答

  • 9

    这是 only answer 设法为我的问题工作,在this网页的帮助下弄清楚了(很好的参考) .

    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}"
    
  • 2

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

    看看 powershell /?

  • 44

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

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

相关问题