Powershell 从每一行读取一些字符串 [英] Powershell to read some strings from each line

查看:38
本文介绍了Powershell 从每一行读取一些字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的要求:

有一个包含以下模式的文本文件

Have a text file containing the following in the following pattern

172.26.xxy.zxy:Administrator:Password
172.26.xxy.yyx:Administrator:Password
172.26.xxy.yyy:Administrator:Password
172.26.xxy.yxy:Administrator:Password

我需要我的 powershell 脚本来读取每个单词并在需要时使用该单词.例如,

I need my powershell script to read each word and use that word whereever required. For example,

foreach(something)
{
 I want the IP's(172.26.---.---) to read and store the value as a variable.
 I want to store the two words after **:** in seperate variables.
}

这是怎么做到的?我知道读取整个文件或获取一些特定的字符串.但我需要在每一行上做同样的事情.任何帮助将不胜感激.

How can this be done? I know to read an entire file or get some specific string. But I need the same to be done on each line.Any help would be really appreciated.

推荐答案

类似这样的事情?您可以在 : 上拆分,然后根据索引存储变量

Something like this? You can just split on the : and then store your variables based on the index

$contents = Get-Content C:\your\file.txt

foreach($line in $contents) {
  $s = $line -split ':'
  $ip = $s[0]
  $user = $s[1]
  $pass = $s[2]
  write-host $ip $user $pass
}

小修改:内容中缺少t".

这篇关于Powershell 从每一行读取一些字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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