在批处理文件中执行powershell脚本

时间:2011-05-30 20:38:20

标签: powershell

  

可能重复:
  How to execute powershell commands from a batch file?

我想从批处理文件中执行以下powershell语句而不创建ps1文件

if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");}
else{write("Event Log already exists");}

是否可以这样做?

2 个答案:

答案 0 :(得分:3)

一般来说,你可以这样做:

 @powershell -command "yourpowershellcommand"

您可以直接使用powershell.exe,但我建议您使用上述@powershell

形式

答案 1 :(得分:1)

命令行帮助powershell.exe /?涵盖了这个:

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed

    in braces ({}). You can specify a script block only when running PowerShell.exe

最后显示了一个例子:

EXAMPLES
    PowerShell -PSConsoleFile SqlSnapIn.Psc1
    PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML
    PowerShell -Command {Get-EventLog -LogName security}
    PowerShell -Command "& {Get-EventLog -LogName security}"