PowerShell中一个分号引发的问题
2012-06-28 16:35
309 查看
今天在用start-process这个cmdlet去新开一个窗口执行powershell的时候遇到的一个问题
看一下测试代码:
以下这个ps代码命名为profile.ps1,并且保存在%UserProfile%\My Documents\WindowsPowerShell这个目录下
它就相当于是运行powershell时自动加载的脚本,代码如下
然后再有一段执行的代码
最后输出的结果是这样的
我本意是想输出
test1;ok
test2
我们可以在d:\t.log文件中看到这样的错误信息
The term 'ok' 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.
At line:1 char:15
+ print test1;ok <<<< test2
+ CategoryInfo : ObjectNotFound: (ok:String) [], CommandNotFoundE
xception
+ FullyQualifiedErrorId : CommandNotFoundException
就是说参数中的分号将它们分开为两个语句了,所以在上下文中并没有定义ok这个东西,所以报这个错。
我尝试以为可以将分号转义,所以这样运行
一样的错误
我再使用双引号试试?
依然是一样的错误
好吧!咱就只能传数组,在函数中自己解决这个问题了!?求高人指点
还有一个问题,就是想上面这样,双引号也是无法传给print这个函数的
就是说我想打印出的结果想是这样的
“test1”
test2
但实际打印的确是
test1
test2
看一下测试代码:
以下这个ps代码命名为profile.ps1,并且保存在%UserProfile%\My Documents\WindowsPowerShell这个目录下
它就相当于是运行powershell时自动加载的脚本,代码如下
function print { [CmdletBinding()] Param( [ValidateNotNullOrEmpty()] $param1="default1" [ValidateNotNullOrEmpty()] $param2="default2" ) write-host $param1 write-host $param2 Read-Host }
然后再有一段执行的代码
function Test-Print { [CmdletBinding()] Param( [ValidateNotNullOrEmpty()] $param1, [ValidateNotNullOrEmpty()] $param2 ) Write-Host $param1 $arguments="print $param1 $param2" start-process -FilePath "$PSHome\powershell.exe" -ArgumentList $arguments -RedirectStandardError d:\t.log } Test-Print "test1;ok" "test2"
最后输出的结果是这样的
我本意是想输出
test1;ok
test2
我们可以在d:\t.log文件中看到这样的错误信息
The term 'ok' 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.
At line:1 char:15
+ print test1;ok <<<< test2
+ CategoryInfo : ObjectNotFound: (ok:String) [], CommandNotFoundE
xception
+ FullyQualifiedErrorId : CommandNotFoundException
就是说参数中的分号将它们分开为两个语句了,所以在上下文中并没有定义ok这个东西,所以报这个错。
我尝试以为可以将分号转义,所以这样运行
Test-Print "test1`;ok" "test2"
一样的错误
我再使用双引号试试?
Test-Print "`"test1;ok`"" "test2"
依然是一样的错误
好吧!咱就只能传数组,在函数中自己解决这个问题了!?求高人指点
还有一个问题,就是想上面这样,双引号也是无法传给print这个函数的
就是说我想打印出的结果想是这样的
“test1”
test2
但实际打印的确是
test1
test2
相关文章推荐
- PowerShell中一个分号引发的问题
- 由作用域链引发的一个小问题
- MDT2012部署问题,一个端口引发的故障
- 安装SQL Server2K可能引发一个严重问题
- Android:一个Multidex引发的VerifyError和Class Not Found问题
- 一个知乎问题引发的血案
- 一个不认真引发的数据无法录入润乾报表中的问题
- Velocity Toolbox类的方法无法调用时引发的一个类型转换问题
- 一个c语言规范引发的一串问题
- 从map的一个优雅写法的问题引发的思考
- 一个office未激活引发的问题
- WinForm:一个登陆窗体引发的问题系列(四):存储过程
- 一个小问题引发的"Listview里有多个Edittext,点击一个全部都高亮"
- 一个Java NIO问题引发的思考
- 由安卓开发中的一个坑引发的问题解决之道浅析
- Sublime Text 写sass的时候,每次都需要输入一个分号的问题的解决方法
- postgres -- 一个问题引发的事务探究(一)
- 一个HTTP请求引发的问题
- 一个VS.net自动生成代码引发的问题
- 记一个由MemCached引发的性能问题