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

  • 相关阅读:
    频偏(转载)
    /proc/interrupts 和 /proc/stat 查看中断的情况 (转载)
    Linux2.6 内核中结构体初始化(转载)
    用grep在子目录中指定的文件类型中查找(转载)
    用C++调用C的库函数(转载)
    H.264(MPEG-4 AVC)级别(Level)、DPB 与 MaxDpbMbs 详解(转载)
    emacs在org-mode时输出pdf时,只输出为链接
    maven 学习---使用Maven构建项目
    maven 学习---Maven构建生命周期
    maven 学习---使用Maven模板创建项目
  • 原文地址:https://www.cnblogs.com/motools/p/3307500.html
Copyright © 2011-2022 走看看