Run PowerShell command from command prompt (no ps1 script)

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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  -Recurse -FileType 

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.

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

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.

回答1:

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

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

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


回答2:

Run it on a single command line like so:

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


回答3:

Maybe powershell -Command "Get-AppLockerFileInformation....."

Take a look at powershell /?



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!