简体   繁体   English

Powershell外文件无法按预期工作

[英]Powershell Out-File Not working as expected

Right, I have a Powershell script which looks like this: 是的,我有一个如下所示的Powershell脚本:

Function GatewayScan
{
cls
$CNList = Import-Csv "c:\Users\user\desktop\scans\ComputerList.csv"
"MachineName, GatewayAddress" | out-file "C:\Users\user\desktop\scans\GatewayList.txt" -append -noClobber
foreach ($line in $CNList) {
    IF (test-connection -comp $($line.machinename) -count 1 -quiet) {
    Write-Host "Geting Gateway for " $($line.machinename)
    $gate = get-wmiobject -cn $($line.machinename) win32_networkadapterconfiguration | where {$_.IPEnabled -eq "TRUE"} | select DefaultIPGateway | format-list
    $($line.machinename) + "," + $gate | out-file "C:\Users\user\desktop\scans\GatewayList.txt" -append -noClobber
    }
    Else {
    $($line.machinename) + ", Offline" | out-file "C:\Users\user\desktop\scans\GatewayList.txt" -append -noClobber
    }
}
}

When I run this, it loops through the machines, and pulls out the Default Gateway info. 当我运行此命令时,它会循环遍历计算机,并拉出“默认网关”信息。 Then it writes this info, along with the machine name to a csv. 然后,它将此信息以及计算机名称写入CSV。 Problem is, when I oppen the text file it produces, I get something which looks like this: 问题是,当我使用它生成的文本文件时,会得到如下所示的内容:

Machinename,Gateway 机器名称,网关
PC1,Microsoft.Powershell.Commands.Internal.Format.FormatStartData PC2,Microsoft.Powershell.Commands.Internal.Format.FormatStartData PC1,Microsoft.Powershell.Commands.Internal.Format.FormatStartData PC2,Microsoft.Powershell.Commands.Internal.Format.FormatStartData

Any help here would be appreciated, I have no idea what I've done to cause this... 这里的任何帮助将不胜感激,我不知道我做了什么导致这种情况...

Thanks, 谢谢,

Remove the pipe to Format-List , it generates formatting instructions. 删除到Format-List的管道,它会生成格式化指令。 Also, make sure you get the raw value of DefaultIPGateway by expanding the property: 另外,通过扩展属性,确保获得DefaultIPGateway的原始值:

... | select -expand DefaultIPGateway

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

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