How to pass a variable in the select-string of powershell

别说谁变了你拦得住时间么 提交于 2019-12-13 06:05:10

问题


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

Please find the below example

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

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

Could any one please help me in this?


回答1:


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


来源:https://stackoverflow.com/questions/17188088/how-to-pass-a-variable-in-the-select-string-of-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!