powershell - 从Windows批处理文件调用Powershell cmdlet

标签 powershell batch-file

好吧,这么简单的事情对我不起作用。我有一个接受单个参数的cmdlet。我试图在Windows批处理文件中调用cmdlet。批处理文件包含:

cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell Set-ExecutionPolicy Unrestricted
powershell 'C:\convert-utf8-to-utf16.ps1 C:\test.txt'
powershell Set-ExecutionPolicy Restricted
pause

我的ps1文件再次没有做任何特殊的事情:
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
}

当我执行bat文件时,它会运行到完成状态(没有错误消息),并且似乎不会创建temp.txt文件。

我可以在PS命令提示符下运行powershell命令文件,但不能在cmd中运行!

任何人都知道有什么问题吗?

最佳答案

从Powershell版本2开始,您可以像这样运行Powershell脚本...

powershell -ExecutionPolicy RemoteSigned -File "C:\Path\Script.ps1" "Parameter with spaces" Parameter2

现在,如果我只能找出一种处理dragging and dropping files to a Powershell script的方法。

关于powershell - 从Windows批处理文件调用Powershell cmdlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/932291/

相关文章:

c++ - 如何从其他exe程序运行Devenv.exe?

windows - powershell批处理管道

powershell - 作为 TFS 构建的一部分,仅从 TFS checkin 文件

wpf - Powershell 数据模板绑定(bind)语法

Azure CLI DevOps 使用 PowerShell 获取创建项目的 ID

java - Install4j:脚本生成器?

windows - 如何使用批处理文件执行任何 .exe?

windows - 在 Windows 上的 bash 中清除 PowerShell 控制台

bash - 如何使用 PowerShell 在 WSL 中启动 Jupyter Notebook?

windows - 在 Windows 7 中查找子文件夹中的文件总数