我正在编写一个脚本来收集有关域计算机的数据,我试图将其输出到csv文件。然而它并没有像我想要的那样出现。
这是我的代码:
Set-ExecutionPolicy Unrestricted -force
Import-Module ActiveDirectory
$CSVPath = $ScriptFolderPath + "\" + $ScriptName + ".csv"
$Import = Get-Content "c:\T2\AD Computers Scripts\ComputersTest.csv"
foreach ($Member in $Import)
{
$ComputerName = Get-ADComputer $Member -Properties Name, CanonicalName | Select Name, CanonicalName
$ConnectionStatus = Test-Connection $Member -Quiet
IF ($ConnectionStatus -eq $TRUE)
{
$IPAddress = Test-Connection $Member -Count 1 | Select -ExpandProperty IPV4Address
}
ELSE
{
$IPAddress = "Not able to contact server"
}
$CSVPath
$Report1 = $ComputerName | ForEach -Process {$_ | Add-Member -Name IPAddress -Value $IPAddress -MemberType NoteProperty -PassThru}
$Report1 | Add-Member -Name Subnet -Value $CSVPath -MemberType NoteProperty -PassThru
$Report1
"`n`n`n"
输出看起来像这样:
Name CanonicalName IPAddress Subnet
---- ------------- --------- ------
CEN-RVS abc.local/Servers/Corpora... 10.19.95.2 C:\t2\AD Computers Scripts
CEN-RVS abc.local/Servers/Corpora... 10.19.95.2 C:\t2\AD Computers Scripts
我所需要的只是一条线。它应该是这样的:
Name CanonicalName IPAddress Subnet
---- ------------- --------- ------
CEN-RVS abc.local/Servers/Corpora... 10.19.95.2 C:\t2\AD Computers Scripts
答案 0 :(得分:2)
-PassThru
Add-Member
参数表示"输出修改后的对象"。
因此,这两个陈述(除了将$Report1
添加NoteProperty的副作用)冗余:
$Report1 | Add-Member -Name Subnet -Value $CSVPath -MemberType NoteProperty -PassThru
$Report1
删除末尾的$Report1
语句或从Add-Member语句中删除-PassThru
开关