输入文件:
"Server1","lanmanserver"
"Server2","lanmanserverTest"
程序
$csvFilename = "D:\Scripts\ServerMonitorConfig.csv"
$csv = Import-Csv $csvFilename -Header @("ServerName","ServiceName")
foreach ($line in $csv) {
Write-Host "ServerName=$line.ServerName ServiceName=$line.ServiceName"
}
我想要的是什么:
Server-Name = Server1 ServiceName = lanmanserver
Server-Name = Server2 ServiceName = lanmanserverT
我得到了什么:
服务器名= @ {服务器名= Server1上;的ServiceName = lanmanserver} .ServerName 的ServiceName = @ {服务器名= Server1上;的ServiceName = lanmanserver} .ServiceN ame ServerName = @ {ServerName = Server2; 的ServiceName = lanmanserverTest} .ServerName 的ServiceName = @ {服务器名=服务器2;的ServiceName = lanmanserverTest}。 服务名称
我真的不在乎Headers是否来自CSV的第一行,我在那里很灵活。
答案 0 :(得分:2)
您通常会看到用于解决该问题的子表达式或格式字符串:
子表达式:
Write-Host "ServerName=$($line.ServerName) ServiceName=$($line.ServiceName)"
格式化字符串:
Write-Host ('ServerName={0} ServiceName={1}' -f $line.ServerName,$line.ServiceName)