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

  • 相关阅读:
    (转)Windows平台 杀死端口号进程
    android 基础知识总结
    使用安卓手机来运行与调试Android应用程序的步骤
    (转)JavaScript的eval(“{JSON object string}”)问题
    (转)Android手机连接电脑详细图文教程
    Nginx配置中FastCGI的几个命令
    设置nginx的超时时间
    Nginx配置中FastCGI的几个命令
    启用django的gzip压缩支持
    启用django的gzip压缩支持
  • 原文地址:https://www.cnblogs.com/motools/p/3307500.html
Copyright © 2011-2022 走看看