如何将带空格的字符串传入 PowerShell? [英] How do I pass in a string with spaces into PowerShell?

查看:40
本文介绍了如何将带空格的字符串传入 PowerShell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

# test1.ps1
param(
    $x = "",
    $y = ""
)

&echo $x $y

像这样使用:

powershell test.ps1

输出:

> <blank line>

但后来出错了:

test.ps1 -x "Hello, World!" -y "my friend"

输出:

Hello,
my

我期待看到:

Hello, World! my friend

推荐答案

嗯,这是一个 cmd.exe 问题,但有一些方法可以解决它.

Well, this is a cmd.exe problem, but there are some ways to solve it.

  1. 使用单引号

  1. Use single quotes

powershell test.ps1 -x 'hello world' -y 'my friend'

  • 使用 -file 参数

    powershell -file test.ps1 -x "hello world" -y "my friend"
    

  • 使用以下内容创建一个 .bat 包装器

    @rem test.bat
    @powershell -file test.ps1 %1 %2 %3 %4
    

    然后调用它:

    test.bat -x "hello world" -y "my friend"
    

  • 这篇关于如何将带空格的字符串传入 PowerShell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆