PowerShell中一个分号引发的问题

摘要:
我尝试以为可以将分号转义,所以这样运行Test-Print"test1`;ok""test2"一样的错误我再使用双引号试试?Test-Print"`"test1;ok`"""test2"依然是一样的错误好吧!求高人指点还有一个问题,就是想上面这样,双引号也是无法传给print这个函数的就是说我想打印出的结果想是这样的“test1”test2但实际打印的确是test1test2

今天在用start-process这个cmdlet去新开一个窗口执行powershell的时候遇到的一个问题

看一下测试代码:

以下这个ps代码命名为profile.ps1,并且保存在%UserProfile%\My Documents\WindowsPowerShell这个目录下

它就相当于是运行powershell时自动加载的脚本,代码如下

functionprint 
{ 
    [CmdletBinding()] 
Param( 
    [ValidateNotNullOrEmpty()] 
    $param1="default1"
    [ValidateNotNullOrEmpty()] 
    $param2="default2") 
    
    write-host $param1write-host $param2Read-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" 

最后输出的结果是这样的

Capture

我本意是想输出

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中一个分号引发的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇P2158 [SDOI2008]仪仗队 线性筛(欧拉函数和素数表)import cx_Oracle ImportError: DLL load failed: 找不到指定的模块。下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

powershell 发邮件

参考文章:https://www.cnblogs.com/lsgxeva/p/9309217.html 文章内容: 问:如何用powershell发邮件?答:Send-MailMessage -Subject "主题" `-From"你的hotmail账户@hotmail.com" -To"你的qq邮箱@qq.com" `-SmtpServer "smtp...

Invoke-WebRequest Invoke-RestMethod 乱码研究

  powershell Invoke-WebRequest Invoke-RestMethod 乱码 encoding sharset CharacterSet       Invoke-WebRequest和Invoke-RestMethod 爬部分网址会乱码。这个问题很久了,很多人知道。似乎从有这两个命令的时候起,就有这个问题,至今已经4年有余了,但...

bat 与 PowerShell 的结合使用

目录 前言 一、使用 bat 做数字比较 二、CMD 命令行 PowerShell 的使用 三、使用 PowerShell 做数字比较 四、调用 PowerShell 的细节及场景 前言 背景: 前前段时间,做文件操作处理时,有这么一个场景: window 下需要对某固定目录下的文件及其他文件进行拷贝操作, 至目标对象---> 外置存储设...

如何用Windows PowerShell替换命令提示符

在Windows 10的“开始”按钮中将PowerShell替换为命令提示符,这不是很好吗?我知道你会有疑问,为什么要这样做?可能会失去了运行DOS命令的能力。好吧,让我解释一下。首先,Windows操作系统中没有DOS这样的东西。有些人错误地将DOS命令提示符称为命令提示符。命令提示符绝对不是DOS提示符。这是一个Windows应用程序,可让您运行我们用...

msi通过powershell安装、卸载

function install_msi($url) { $telemetry = @{ DisplayName = "Telemetry Service"; filename = "TelemetryServiceInstaller_x64.msi" Url = $url } Write-Host $tel...

Windows怎么从命令行下载文件

其具体步骤如下: 1.打开cmd.exe Win+R或者git的bush界面 2.启动powershell 2.1 在命令行中输入 start  powershell 启动powershell 3.下载操作 3.1在powershell.exe文件中输入 $client=new-object System.Net.WebClient 3.2之后再输入...