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

问题描述:

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

这里是我想用来启动用命令:

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

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

+1

一方面你说“我真的不想创建一个脚本“,另一方面你说”如果我只能从批处理文件运行一个或两个命令“。所以:这是它? – djangofan

+0

我应该澄清,我不想创建一个PowerShell脚本。 –

+0

当从新的干净的PowerShell提示运行时,Get-AppLockerFileInformation对你来说工作正常吗?什么OS? (在Win7中测试Get-App ..并且在这种情况下没有这样的cmdlet,因此得到相同的错误消息) – NiKiZe

这里是只能回答​​是设法为我的问题的工作,在网页this的帮助下找到了它(很好的参考)。

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

而且,这里是一个整洁的方式做多个命令:

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

例如,我这是怎么跑到我的两个命令:

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

下面是另一个有用的技巧,你可以从命令提示符运行一个ps1脚本,并像下面这样传递参数:'powershell。 exe文件“C:\ myfile.ps1”arg1 arg2 arg3'或'powershell.exe -File“C:\ myfile.ps1”-Some-Command arg1 -Another-Command arg2' –

+2

命令'powershell -command“& {&'导入模块'AppLocker}“; “&{&Set-AppLockerPolicy'-XmlPolicy myXmlFilePath.xml}”'在单独的PS会话中运行这2个PS命令,即它不适用如果您需要在2个命令之间共享某个变量。解决方法如下:'powershell -command“&{$ someVar ='test';&'Write-Host'$ someVar}”' –

+0

@Maciej我不认为它运行在不同的PS会话,否则第二部分这个特定的命令会失败,因为我需要从命令的第一部分加载该模块。我不同意。但是,正如你所提到的,如果需要的话,共享变量可能有一定的价值。 –

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

看看powershell /?

+0

这就是我一直在尝试,但我似乎并没有得到语法非常正确或什么的。当我直接从powershell运行命令时,它工作正常,但... –

+0

好吧,你有任何错误或其他什么?请编辑您的问题并提供更多信息。你有什么尝试,什么不工作等。 – NiKiZe

+0

请参阅我上面的编辑。 –

运行一个单一命令行上像这样:

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

只要我有机会,我会尝试一下。这里似乎有很多标志,如果我想使用Set-AppLockerPolicy和New-AppLockerPolicy,它会是一样的吗? –

+0

这没有奏效,更不用说-WindowStyle标志关闭我的命令提示符了......此外,我不需要ExecutionPolicy标志,因为我直接使用命令行。 –

+0

是的,有些选项可能是不必要的。只需调整,取决于你需要什么。这里有更多的信息:http://zduck.com/2012/powershell-batch-files-exit-code/ – djangofan