PowerShell中一个分号引发的问题
2012-06-28 16:27
459 查看
今天在用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
本文出自 “DailyReport” 博客,请务必保留此出处http://lockrock.blog.51cto.com/2147255/911794
看一下测试代码:
以下这个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
本文出自 “DailyReport” 博客,请务必保留此出处http://lockrock.blog.51cto.com/2147255/911794
相关文章推荐
- PowerShell中一个分号引发的问题
- 由一个C++问题引发的讨论和思考(未完待续)
- 一个多线程问题引发的血案-(代码段执行完毕,子进程未执行完毕导致段错误)
- 一个VS.net自动生成代码引发的问题
- 一个YAFFS引发的一系列问题
- 【杂症】一个豌豆荚引发的血案——关于ADB server didn't ACK的问题
- 一个问题引发对Linux swap和内存的思考
- 项目登录不进去的问题(一个跳转引发的血案)
- 由一个问题引发的思考
- 论坛上的一个问题引发的(修改中)
- 一个分号引发的血案,中文登录正常,英文登录挂掉
- 一个JavaScript问题引发的思考
- android开发之一个手机重启问题引发的思考
- 一个逗号引发的问题 js错误
- 一个JDK版本问题引发的思考--Java环境配置 && Eclipse的JDK配置
- 安装SQL Server2K可能引发一个严重问题(2)
- getYear引发的一个小问题
- 一个游戏引发的思考(概率问题)
- 一个由安装顺序引发的域登录问题
- 由一个简单的String c=a+b的Java问题引发一点想法