zoukankan      html  css  js  c++  java
  • Powershell 字符串处理案例

    有一张Excel表格收集了计算机名和IP地址,另外一张表有计算机名,需要找出这张表中计算机名对应的IP地址。

    #定义函数Get-LikeContentInfo
    function Get-LikeContentInfo {
        param(
        [ValidateNotNullOrEmpty()] [string]$CSVPath,    #参数非空,输入CSV文件路径
        [ValidateNotNullOrEmpty()] [string]$InputPath,  #参数非空,输入TXT文件路径
        [ValidateNotNullOrEmpty()] [string]$OutCSVPath  #参数非空,输出结果CSV文件路径
        )
        $CSVFile = Import-Csv -Path  $CSVPath -Encoding oem   #定义变量$CSVFile存放CSV文件内容
        $TXTFile = Get-Content $InputPath                     #定义变量$TXTFile存放TXT文件内容
        $Result = @()        #定义数组变量$Result
        #执行对比,将TXT每行内容和CSV文件内容对比,如果一致则输入结果
        for ($index = 0; $index -le ($CSVFile.Length - 1); $index++) 
        {
            if($TXTFile -contains ($CSVFile[$index].ComputerName) -eq $true )
            {
              $NameIP = New-Object -TypeName PSObject
              $NameIP | Add-Member NoteProperty ComputerName $CSVFile[$index].ComputerName
              $NameIP | Add-Member NoteProperty IP    $CSVFile[$index].IP
              $Result += $NameIP    
            }
        }
        $Result | Export-Csv -Path $OutCSVPath -NoTypeInformation
    }
     
    Get-LikeContentInfo -CSVPath c:dns.csv -InputPath c:computername.txt -OutCSVPath e:a.csv  #调用函数Get-LikeContentInfo

    dns.csv文件

    image

    computername.txt

    image

    输出结果:

    image

  • 相关阅读:
    发送 GET 和 POST 请求
    日志记录帮助类
    常用正则表达式
    获取验证码
    C# 生成二维码
    android sql Cursor
    sql 语句操作
    android 界面悬浮框实现
    android activity四种启动模式
    andorid 自定义view属性declare-styleable
  • 原文地址:https://www.cnblogs.com/motools/p/3307500.html
Copyright © 2011-2022 走看看