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

  • 相关阅读:
    ios状态栏
    RGBA设置颜色
    应用程序的生命周期(转)
    UIViewController的生命周期
    UIViewController的创建
    UIButton
    NSUserDefaults
    打印结构体
    iOS 界面间的传值 属性传值 代理传值
    如何安装Homebrew
  • 原文地址:https://www.cnblogs.com/motools/p/3307500.html
Copyright © 2011-2022 走看看