【发布时间】:2015-07-23 20:48:39
【问题描述】:
我是 PowerShell 的新手,我正在尝试编写一个脚本来查询 AD 的机器名称,检查哪些正在响应并将输出写入文件。到目前为止,我有这个:
$computers = Get-ADComputer -filter {(Name -like "PC*")} | Select-Object -Property Name
foreach ($computer in $computers) {
if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -Ea 0 -Quiet)) {
"Machine $computer connected." | Out-File "out.txt" -Append
} else {
"Machine $computer not connected." | Out-File "out.txt" -Append
} #end if
} #end foreach
我在输出文本文件中得到的内容如下所示:
...
Machine @{Name=PC-0649} not connected.
Machine @{Name=PC-1541} not connected.
Machine @{Name=PC-1574} not connected.
...
我认为我的问题在于第一行的Select-Object -Property Name 部分。运行调试器,PowerShell 似乎正在格式化 $computer 的每个迭代以包含标题行。
[DBG]: PS Y:\>> $computer
Name
----
PC-0649
在这种情况下,除了 PC-#### 部分之外,我最好的方法是什么?
【问题讨论】:
标签: powershell active-directory powershell-4.0