调用从Windows批处理文件PowerShell命令 [英] Calling powershell cmdlets from Windows batch file

查看:809
本文介绍了调用从Windows批处理文件PowerShell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好这么简单的东西只是不为我工作。我有一个接受单个参数cmdlet的。我试图调用Windows批处理文件中的一个小命令。批处理文件包含:

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 file contains:

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文件中再次没有做什么特别的:

My ps1 file again not doing anything special:

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
  }

当我执行批处理文件,它只是运行至完成(没有错误消息),它似乎并不创建TEMP.TXT文件。

When i execute the bat file it just runs to completion (no error messages) and it does not appear to create the temp.txt file.

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

I can run the powershell command file at the PS command prompt but not in cmd!

任何人有什么想法可能是错了吗?

Anyone got any ideas what could be wrong?

感谢

推荐答案

使用PowerShell 2.0版本开始,你可以运行PowerShell脚本像这样...

Starting with Powershell version 2, you can run a Powershell script like so...

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

现在,如果我只能想出一个办法来处理文件拖放到PowerShell脚本

Now if I could only figure out a way to handle dragging and dropping files to a Powershell script.

这篇关于调用从Windows批处理文件PowerShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆