简体   繁体   English

使用Powershell读取Excel文件

[英]Read Excel File with Powershell

I've got a excel serverlist where column D is hostnames and column F area (like dev, int, prd). 我有一个excel服务器列表,其中D列是主机名,F列是区域(例如dev,int,prd)。 Now I want to read this excelfile with powershell and printout each hostname that has the area dev. 现在,我想使用powershell读取此excelfile并打印出具有区域dev的每个主机名。 What do I have to change? 我必须改变什么?

$FilePath = "C:\serverlist.xlsx"
$SheetName = "serverlist"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($FilePath)
$WorkSheet = $WorkBook.sheets.item($SheetName)


$Range = $WorkSheet.Range("F2:F150").Text

Foreach ($cell in $Range) {

    If ($cell -like "dev") {

        Write-Host "hostname from  Column D"

    }

}

Save your spreadsheet as a CSV and you will be able to process the list much faster. 保存您的电子表格为CSV,你将能够以更快的速度处理名单。 Then you can use following code, change values after -Header to column names relevant to you: 然后,您可以使用以下代码,将-Header之后的值-Header为与您相关的列名称:

$FilePath = "C:\serverlist.csv"
$serverList = Import-Csv $FilePath 
    -Header "ColumnA","ColumnB","ColumnC","HostName","Area"
$serverList | where {$_.Area -like"dev" } | Select-Object HostName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM