How to execute powershell commands from a batch file?

后端 未结 5 799
暖寄归人
暖寄归人 2020-12-04 16:47

I have a PowerShell script to add a website to a Trusted Sites in Internet Explorer:

set-location \"HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Inte         


        
相关标签:
5条回答
  • 2020-12-04 16:57

    This solution is similar to walid2mi (thank you for inspiration), but allows the standard console input by the Read-Host cmdlet.

    pros:

    • can be run like standard .cmd file
    • only one file for batch and powershell script
    • powershell script may be multi-line (easy to read script)
    • allows the standard console input (use the Read-Host cmdlet by standard way)

    cons:

    • requires powershell version 2.0+

    Commented and runable example of batch-ps-script.cmd:

    <# : Begin batch (batch script is in commentary of powershell v2.0+)
    @echo off
    : Use local variables
    setlocal
    : Change current directory to script location - useful for including .ps1 files
    cd %~dp0
    : Invoke this file as powershell expression
    powershell -executionpolicy remotesigned -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
    : Restore environment variables present before setlocal and restore current directory
    endlocal
    : End batch - go to end of file
    goto:eof
    #>
    # here start your powershell script
    
    # example: include another .ps1 scripts (commented, for quick copy-paste and test run)
    #. ".\anotherScript.ps1"
    
    # example: standard input from console
    $variableInput = Read-Host "Continue? [Y/N]"
    if ($variableInput -ne "Y") {
        Write-Host "Exit script..."
        break
    }
    
    # example: call standard powershell command
    Get-Item .
    

    Snippet for .cmd file:

    <# : batch script
    @echo off
    setlocal
    cd %~dp0
    powershell -executionpolicy remotesigned -Command "Invoke-Expression $([System.IO.File]::ReadAllText('%~f0'))"
    endlocal
    goto:eof
    #>
    # here write your powershell commands...
    
    0 讨论(0)
  • 2020-12-04 17:01

    Type in cmd.exe Powershell -Help and see the examples.

    0 讨论(0)
  • 2020-12-04 17:10

    This is what the code would look like in a batch file(tested, works):

    powershell -Command "& {set-location 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'; set-location ZoneMap\Domains; new-item SERVERNAME; set-location SERVERNAME; new-itemproperty . -Name http -Value 2 -Type DWORD;}"
    

    Based on the information from:

    http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/

    0 讨论(0)
  • 2020-12-04 17:20

    untested.cmd

    ;@echo off
    ;Findstr -rbv ; %0 | powershell -c - 
    ;goto:sCode
    
    set-location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    set-location ZoneMap\Domains
    new-item TESTSERVERNAME
    set-location TESTSERVERNAME
    new-itemproperty . -Name http -Value 2 -Type DWORD
    
    ;:sCode 
    ;echo done
    ;pause & goto :eof
    
    0 讨论(0)
  • 2020-12-04 17:21

    Looking for the possibility to put a powershell script into a batch file, I found this thread. The idea of walid2mi did not worked 100% for my script. But via a temporary file, containing the script it worked out. Here is the skeleton of the batch file:

    ;@echo off
    ;setlocal ENABLEEXTENSIONS
    ;rem make from X.bat a X.ps1 by removing all lines starting with ';' 
    ;Findstr -rbv "^[;]" %0 > %~dpn0.ps1 
    ;powershell -ExecutionPolicy Unrestricted -File %~dpn0.ps1 %*
    ;del %~dpn0.ps1
    ;endlocal
    ;goto :EOF
    ;rem Here start your power shell script.
    param(
        ,[switch]$help
    )
    
    0 讨论(0)
提交回复
热议问题