My Powershell script, Foo.ps1:
我的Powershell脚本,Foo.ps1:
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo($args[0])
}
From the Windows console:
从Windows控制台:
powershell -command .\Foo.ps1
Results in: "The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
结果:“术语'。\ Foo.ps1'未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是正确的,然后再试一次。“
This is despite Foo.ps1 being in the current directory from where I am calling Powershell. I then tried to call it specifying the full path to the script file; because this path contains a space I believe I have to quote it in some way. Also I need to pass an argument to the script which is also a file name that contains one or more spaces. No matter what I try I can't get it to work. This is my best guess so far:
尽管Foo.ps1位于我调用Powershell的当前目录中。然后我试着调用它来指定脚本文件的完整路径;因为这条路径包含一个空间我相信我必须以某种方式引用它。此外,我需要将参数传递给脚本,该脚本也是包含一个或多个空格的文件名。无论我尝试什么,我都无法让它发挥作用。到目前为止,这是我最好的猜测:
powershell -command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
Which gives the error "Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement. At line:1 char:136".
这在表达式或语句中给出了错误“Unexpected token'C:\ Dummy Directory 2 \ File.txt'。在第1行:char:136”。
Edit: I have worked out why
编辑:我找出了原因
powershell -command .\Foo.ps1
did not work. This was because my Microsoft.PowerShell_profile.ps1 file had
不工作。这是因为我的Microsoft.PowerShell_profile.ps1文件有
cd C:\
cd C:\
so as soon as powershell was starting up it was changing directory.
所以,一旦powershell启动它就会改变目录。
5 个解决方案
#1
33
try this:
尝试这个:
powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"
#2
32
you are calling a script file not a command so you have to use -file eg :
您正在调用脚本文件而不是命令,因此您必须使用-file,例如:
powershell -executionPolicy bypass -noexit -file "c:\temp\test.ps1" "c:\test with space"
for PS V2
对于PS V2
powershell.exe -noexit &'c:\my scripts\test.ps1'
(check bottom of this technet page http://technet.microsoft.com/en-us/library/ee176949.aspx )
(查看此technet页面的底部http://technet.microsoft.com/en-us/library/ee176949.aspx)
#3
15
Using the flag -Command
you can execute your entire powershell line as if it was a command in the PowerShell prompt:
使用标志-Command,您可以执行整个PowerShell行,就好像它是PowerShell提示符中的命令一样:
powershell -Command "& '<PATH_TO_PS1_FILE>' '<ARG_1>' '<ARG_2>' ... '<ARG_N>'"
This solved my issue with running PowerShell commands in Visual Studio Post-Build and Pre-Build events.
这解决了我在Visual Studio Post-Build和Pre-Build事件中运行PowerShell命令的问题。
#4
2
Change your code to the following :
将您的代码更改为以下内容:
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo([string[]]$args)
}
And then invoke it as:
然后将其调用为:
powershell -ExecutionPolicy RemoteSigned -File "c:\foo.ps1" "c:\Documents and Settings" "c:\test"
powershell -ExecutionPolicy RemoteSigned -File“c:\ foo.ps1”“c:\ Documents and Settings”“c:\ test”
#5
1
Add the param declation at the top of ps1 file
在ps1文件的顶部添加param declation
test.ps1
test.ps1
param(
# Our preferred encoding
[parameter(Mandatory=$false)]
[ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
[string]$Encoding = "UTF8"
)
write ("Encoding : {0}" -f $Encoding)
result
结果
C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII
#1
33
try this:
尝试这个:
powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"
#2
32
you are calling a script file not a command so you have to use -file eg :
您正在调用脚本文件而不是命令,因此您必须使用-file,例如:
powershell -executionPolicy bypass -noexit -file "c:\temp\test.ps1" "c:\test with space"
for PS V2
对于PS V2
powershell.exe -noexit &'c:\my scripts\test.ps1'
(check bottom of this technet page http://technet.microsoft.com/en-us/library/ee176949.aspx )
(查看此technet页面的底部http://technet.microsoft.com/en-us/library/ee176949.aspx)
#3
15
Using the flag -Command
you can execute your entire powershell line as if it was a command in the PowerShell prompt:
使用标志-Command,您可以执行整个PowerShell行,就好像它是PowerShell提示符中的命令一样:
powershell -Command "& '<PATH_TO_PS1_FILE>' '<ARG_1>' '<ARG_2>' ... '<ARG_N>'"
This solved my issue with running PowerShell commands in Visual Studio Post-Build and Pre-Build events.
这解决了我在Visual Studio Post-Build和Pre-Build事件中运行PowerShell命令的问题。
#4
2
Change your code to the following :
将您的代码更改为以下内容:
Function Foo($directory)
{
echo $directory
}
if ($args.Length -eq 0)
{
echo "Usage: Foo <directory>"
}
else
{
Foo([string[]]$args)
}
And then invoke it as:
然后将其调用为:
powershell -ExecutionPolicy RemoteSigned -File "c:\foo.ps1" "c:\Documents and Settings" "c:\test"
powershell -ExecutionPolicy RemoteSigned -File“c:\ foo.ps1”“c:\ Documents and Settings”“c:\ test”
#5
1
Add the param declation at the top of ps1 file
在ps1文件的顶部添加param declation
test.ps1
test.ps1
param(
# Our preferred encoding
[parameter(Mandatory=$false)]
[ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
[string]$Encoding = "UTF8"
)
write ("Encoding : {0}" -f $Encoding)
result
结果
C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII