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

  • 相关阅读:
    android 中ImageButton按下改变背景图片的效果
    Android根据Button状态(normal,focused,pressed)显示不同背景图片
    Android简单逐帧动画Frame的实现(三)
    Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus
    美团点评云真机平台实践
    美团客户端响应式框架EasyReact开源啦
    MCI:移动持续集成在大众点评的实践
    如何基于深度学习实现图像的智能审核?
    Android自动化页面测速在美团的实践
    美团外卖iOS多端复用的推动、支撑与思考
  • 原文地址:https://www.cnblogs.com/motools/p/3307500.html
Copyright © 2011-2022 走看看