简体   繁体   English

从批处理文件调用PowerShell脚本

[英]Calling a PowerShell script from a batch file

I'm trying to run a PowerShell script from a batch file, and it runs successfully on my machine. 我正在尝试从批处理文件运行PowerShell脚本,并且该脚本在我的计算机上成功运行。 However when I put it on the server, it throws this error: 但是,当我将其放在服务器上时,它将引发以下错误:

"Missing expression after unary operator '-'. “缺少一元运算符'-'之后的表达式。
At line:1 char:2 行:1字符:2
+ -E <<<< xecutionPolicy remotesigned -file 'E:\\Misdemeanor + -E <<<< xecutionPolicy remotesigned -file'E:\\ Misdemeanor
Queue\\Concatenate.ps1' Queue \\ Concatenate.ps1'

I've tried different combinations of the execution policy, but nothing seems to work. 我尝试了执行策略的不同组合,但似乎没有任何效果。 I have to run this on Windows Server 2003, SP2, and it uses v1.0 of PowerShell (which is what I wrote this in). 我必须在Windows Server 2003 SP2上运行它,并且它使用PowerShell的v1.0(这就是我在其中编写的内容)。 There is a space in the folder name that houses the batch file & ps1 file. 文件夹名称中有一个空格,用于容纳批处理文件和ps1文件。 Here is the batch file that calls the ps1 file named Concatenate.ps1: 这是调用名为Concatenate.ps1的ps1文件的批处理文件:

 - @echo off &setlocal
del "E:\Misdemeanor Queue\Index3.txt"
set "search=.tif"
set "replace=|"
set "textfile=index2.txt"
set "newfile=Index3.txt"

(for /f "delims=" %%i in ('findstr "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%"


PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'E:\Misdemeanor Queue\Concatenate.ps1'"

Here is the concatenate.ps1 file: 这是concatenate.ps1文件:

$File1 = Get-Content "E:\\Misdemeanor Queue\\Index3.txt" $File2 = Get-Content "E:\\Misdemeanor Queue\\index1.txt" $File3 = @() For($a=0;$a -lt $File1.count;$a++){ $File3+=$File1[$a].trim()+$File2[$a].trim() } $File3 | $ File1 =获取内容“ E:\\ Misdemeanor Queue \\ Index3.txt” $ File2 =获取内容“ E:\\ Misdemeanor Queue \\ index1.txt” $ File3 = @()For($ a = 0; $ a- lt $ File1.count; $ a ++){$ File3 + = $ File1 [$ a] .trim()+ $ File2 [$ a] .trim()} $ File3 | Out-File "E:\\Misdemeanor Queue\\indexFINAL.txt" 输出文件“ E:\\ Misdemeanor Queue \\ indexFINAL.txt”


It's supposed to call this ps file, concatenate the Index3 file and the Index1 file, and create a txt file called indexFINAL. 应该调用此ps文件,将Index3文件和Index1文件连接起来,并创建一个名为indexFINAL的txt文件。

What should I do? 我该怎么办?

That's the error you would get on PowerShell 1.0 where the -ExecutionPolicy parameter is not supported. 那就是您在不支持-ExecutionPolicy参数的PowerShell 1.0上-ExecutionPolicy的错误。 So I suspect your server has version 1.0 installed. 因此,我怀疑您的服务器已安装1.0版。 If a PowerShell prompt comes up with a copyright date of 2006 then it is version 1.0. 如果PowerShell提示的版权日期为2006,则为1.0版本。 Can you upgrade the server to at least v2 to get support for that parameter? 您可以将服务器至少升级到v2以获得对该参数的支持吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM