-2

Possible Duplicate:
How to execute powershell commands from a batch file?

I want to execute the below powershell statement from a batch file with out creating a ps1 file

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

Is it possible to do so?

1

2 Answers 2

3

In general you can do:

 @powershell -command "yourpowershellcommand"

You can use powershell.exe directly, but I would recomment you to use the above form of @powershell

Sign up to request clarification or add additional context in comments.

3 Comments

Guys it doesnt work. I tried doing something like this powershell -command 'if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");}else{write("Event Log already exists");}'
The @ only causes the command not to be echoed if echo is on. In most batch files it's off anyway. There is no functional difference between the invocation with or without the @.
When you have your script in a file, the following works fine: @powershell Invoke-Expression Hello_World.ps1
1

The command line help powershell.exe /? covers this:

-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

It shows an example at the end:

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

2 Comments

Guys it doesnt work. I tried doing something like this powershell -command 'if([System.Diagnostics.EventLog]::SourceExists("dfgdjg") -eq $false){[System.Diagnostics.EventLog]::CreateEventSource("dfgdjg","dfgdjgLogs");‌​}else{write("Event Log already exists");}'
@user: Read the help entry again. Then surround your command with &{...}. And then try again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.