Calling powershell cmdlets from Windows batch file

后端 未结 6 1120
青春惊慌失措
青春惊慌失措 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

    The problem is in the ps1 file - you declare a function but you don't call it. I would modify it like this:

    param($path)
    function convert-utf8-to-utf16 {   
     $tempfile = "C:\temp.txt"
     set-ExecutionPolicy Unrestricted
     get-content -Path $args[0] -encoding utf8 | out-file $tempfile -encoding Unicode
     set-ExecutionPolicy Restricted
    }
    
    convert-utf8-to-utf16 $path
    

    it will work. However, it is not needed, you can simply ommit the function declaration and move the body into the script itself:

    param($path)
    $tempfile = "C:\temp.txt"
    set-ExecutionPolicy Unrestricted
    get-content -Path $path -encoding utf8 | out-file $tempfile -encoding Unicode
    set-ExecutionPolicy Restricted
    

提交回复
热议问题