Calling powershell cmdlets from Windows batch file

后端 未结 6 1142
青春惊慌失措
青春惊慌失措 2020-12-08 21:14

Ok something so simple is just not working for me. I got a cmdlet that accepts a single parameter. I am trying to call a cmdlet within a Windows batch file. The batch fil

6条回答
  •  旧巷少年郎
    2020-12-08 21:36

    # Test-Args.ps1
    param($first, $second)
    write-host $first
    write-host $second
    

    Call from Command Prompt:

    PowerShell.exe -NoProfile -Command "& {./Test-Args.ps1 'C:\Folder A\One' 'C:\Folder B\Two'}"
    

    What's confusing is that if the script is in a folder path containing spaces, PowerShell doesn't recognize the script name in quotes:

    PowerShell.exe -NoProfile -Command "& {'C:\Folder X\Test-Args.ps1' 'C:\Folder
     A\One' 'C:\Folder B\Two'}"
    

    But you can get around that using something like:

    PowerShell.exe -NoProfile -Command "& {set-location 'C:\Folder X';./Test-Args.ps1 'C:\Folder
     A\One' 'C:\Folder B\Two'}"
    

    Don't use spaces in your .PS1 file name, or you're outta luck.

提交回复
热议问题