none
How can I run the below Powershell code in bacth file. RRS feed

  • Question

  • Can any one help me on below:

    How can I run the below Powershell code in bacth file.

    ---------

    Get-WmiObject -Class win32_product | Where-Object {($_.Name -eq "MSystem" -and $_.version -eq "1.1")} | select __Server , name , version|`
    Out-File c:\temp\Mess.txt
    Invoke-Expression c:\temp\Mess.txt

    Get-WmiObject -Class win32_product | Where-Object {($_.Name -eq "MSystem11" -and $_.version -eq "1.2")} | select __Server , name , version|`
    Out-File c:\temp\Mess.txt
    Invoke-Expression c:\temp\Mess.txt

    ------------


    So far I tried below and saved this as .bat file , but cannot see results

    ---------------

    @echo off

    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Get-WmiObject -Class win32_product | select __Server, Name , version | Where-Object {($_.Name -eq "MSystem" -and $_.version -eq "1.1"))} | Out-File c:\temp\MS_Info.txt};
    Invoke-Expression c:\temp\Mess.txt "; 

    Please help


    Best Regards,SQLBoy

    • Moved by Bill_Stewart Tuesday, December 11, 2018 10:17 PM Unanswerable drive-by question
    Wednesday, August 1, 2018 7:02 AM

All replies

  • Place you commands in a file with the "PS1" extension and runlike this:

    powershell -file yourfile.ps1


    \_(ツ)_/

    Wednesday, August 1, 2018 7:11 AM
  • Thanks for the response .

    Can I not directly insert into the bacth file itself and call the powershell code inside ?

    Thanks


    Best Regards,SQLBoy

    Wednesday, August 1, 2018 7:57 AM
  • powershell -command "Get-WmiObject -Class win32_product | Where-Object {($_.Name -eq MSystem -and $_.version -eq 1.1)} | select __Server , name , version| Out-File c:\temp\Mess.txt Invoke-Expression c:\temp\Mess.txt"
    powershell -command "Get-WmiObject -Class win32_product | Where-Object {($_.Name -eq MSystem -and $_.version -eq 1.1)} | select __Server , name , version| Out-File c:\temp\Mess.txt Invoke-Expression c:\temp\Mess.txt"


    Best regards Brian Lind

    Wednesday, August 1, 2018 11:01 AM