我正试图弄清楚如何以下列格式将以下内容输出到日志文件($ log):
www.mydomain1.com : <the dig output stuff>
www.mydomain2.com : <the dig output stuff>
etc..
但是当我尝试下面的脚本时,我只得到以下格式(缺少写输出/域名):
<the dig output stuff>
如何将这两个项目打印到同一行?
ForEach ($domain in $domains) {
$cmd = "c:\Tools\dig\dig.exe ``@8.8.8.8 $domain +short +nssearch"
Write-Output $domain : >> $log
Invoke-Expression $cmd >> $log
}
答案 0 :(得分:3)
未经测试,但应该只能在变量中捕获命令的结果,并使用format运算符构建输出字符串。我们还使用Add-Content
来获得更清晰的代码解决方案。也不需要Invoke-Expression
但可能需要稍微更改字符串。
ForEach ($domain in $domains) {
$results = & "c:\Tools\dig\dig.exe" "@8.8.8.8 $domain +short +nssearch"
('{0} : {1}' -f $domain,$results) | Add-Content $log
}
问题就是你的方式是你应该在两行上得到你想要的数据,这不是你想要的。我认为您甚至没有看到的原因是您的输出行应该是单个字符串Write-Output "$domain :" >> $log
。