【发布时间】:2019-09-11 18:12:10
【问题描述】:
我需要您对我的 PowerShell 脚本的帮助和建议,已经有可以在终端屏幕上显示它的初始程序,我的问题是我想将终端屏幕的输出导出到 CSV 文件
computername.txt里面:
输出终端画面:
PS C:\Users\ksaycon\Desktop> ./script.ps1 d-pol-abanaga 没有 Skype for Business d-pol-pcspc 不存在 Skype for Business d-pol-eplete 没有 Skype for Business这应该是基于终端输出的导出 CSV:
d-pol-abanaga No Skype for Business are present
d-pol-pcspc No Skype for Business are present
d-pol-eplete No Skype for Business are present
代码:
# Read input file of computer names
$computers = Get-Content C:\pstool\computername.txt
# Loop through each computer name in the array
foreach ($computer in $computers) {
# Test for lync.exe file
$path = Test-Path "<\\$computer\c$\program files\microsoft office\office15\*>" -Include lync.exe
# Report if templates are found or not
if ($path -eq $true) {
Write-Output $Computer 'Skype for Business are present' |
Export-Csv -Path "output.csv" -NoTypeInformation -Append
} else {
if ($path -eq $false) {
$path16 = Test-Path "<\\$computer\c$\program files\microsoft office\office16\*>" -Include lync.exe
}
}
if ($path16 -eq $true) {
Write-Output $Computer 'Skype for Business are present' |
Export-Csv -Path "output.csv" -NoTypeInformation -Append
} else {
Write-Output $Computer ' No Skype for Business are present' |
Export-Csv -Path "output.csv" -NoTypeInformation -Append
}
}
【问题讨论】:
-
从路径中删除尖括号。此外,运行脚本的用户是否在远程计算机上具有管理员权限?否则他们无权访问管理共享 (
\\$computer\C$)。 -
谢谢,但我的问题是当导出到 CSV 时它有空数据,没有复制 csv 文件中的确切输出。 :(
-
那么请edit您的问题并显示minimal reproducible example。当您的问题无法让我们理解或重现您所面临的问题时,我们无法为您提供帮助。
-
@AnsgarWiechers 已经编辑,如果我的问题有太多华丽的东西,我很抱歉。我不得不多次表达。请理解我。谢谢
-
取决于您要导出的内容。您可以在循环中使用
Export-Csv -Append,但您不能将Write-Host输出导出到文件,因为该输出会直接发送到主机控制台。如果您希望通过管道或重定向处理输出,请使用Write-Output而不是Write-Host。
标签: windows powershell scripting