如何在powershell的选择字符串中传递变量 [英] How to pass a variable in the select-string of powershell

查看:25
本文介绍了如何在powershell的选择字符串中传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个要求,我需要将在 powershell 中设置的变量传递给另一个变量

There is one requirement where i need to pass a variable that is set in the powershell to the another variable

请找出下面的例子

    PS D:\temp\arun> $match1=get-date -format ddMMM
    PS D:\temp\arun> $match1
    19Jun

test.txt 文件有以下内容

test.txt file has the below content

    19Jun1
    19Jun2
    19Jun3

    PS D:\temp\arun> $match2=select-string -simple 19Jun test.txt
    PS D:\temp\arun> $match2
    19Jun1
    19Jun2
    19Jun3

但是当我尝试传递变量时,我没有得到结果.

But when i am trying to pass the variable i am not getting the result.

    PS D:\temp\arun> $match2=select-string -simple $match1 test.txt
    PS D:\temp\arun> $match2

有人可以帮我吗?

推荐答案

在这里工作正常.尝试指定参数以确保将正确的值分配给正确的参数.

Works fine here. Try specifying the paramters to make sure the right value is assigned to the right parameter.

$match2 = Select-String -SimpleMatch -Pattern $match1 -Path .\test.txt

这篇关于如何在powershell的选择字符串中传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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