我在此行的文本文件中有2个计算机名称 - get-content -path' c:\ powershell \ computernames.txt'。但是,它只将一个计算机名输出到testADcomputers.csv
function Get-loginTest.ps1 {
#[CmdletBinding()]
param (
$computername = (get-content -path 'c:\powershell\computernames.txt')
)
[string]$ErrorLog = 'c:\powershell\useful\errorlog\retry.txt',
[switch]$LogErrors
foreach ($computer in $computername)
{
$os = Get-WmiObject Win32_OperatingSystem -computer $computer
$bios = Get-WmiObject Win32_BIOS -computer $computer
$model = Get-WmiObject Win32_ComputerSystem -computer $computer
$AD = Get-ADComputer $computer -Properties LastLogonDate
$ping = Test-Connection -quiet -computername $computer
}
$obj = New-Object -TypeName PSObject
$obj| Add-Member -MemberType NoteProperty -Name Computername -Value $computer
$obj| Add-Member -MemberType NoteProperty -Name OnlineStatus -value $ping
$obj| Add-Member -MemberType NoteProperty -Name LoggedIn -Value ($model.username)
$obj| Add-Member -MemberType NoteProperty -Name OSVersion -Value ($os.Caption)
$obj| Add-Member -MemberType NoteProperty -Name LastLogonDate -Value ($AD.LastLogonDate)
Write-Output $obj
}
Get-loginTest.ps1 | Export-Csv C:\PowerShell\TestADcomputers.csv
答案 0 :(得分:1)
很简单,你只是告诉它输出1个对象。该对象是在ForEach循环之后生成的,因此它只能看到循环的最后一个循环的结果。你想要做的是在ForEach循环中移动对象创建,并拥有一个包含这些对象的数组。然后,一旦ForEach循环完成,您就可以将其输出到CSV。这个稍微修改过的脚本应该做你想要的:
#[CmdletBinding()]
param (
$computername = (get-content -path 'c:\powershell\computernames.txt'),
[string]$ErrorLog = 'c:\powershell\useful\errorlog\retry.txt',
[switch]$LogErrors
)
[Array]$Collection = foreach ($computer in $computername){
$os = Get-WmiObject Win32_OperatingSystem -computer $computer
$bios = Get-WmiObject Win32_BIOS -computer $computer
$model = Get-WmiObject Win32_ComputerSystem -computer $computer
$AD = Get-ADComputer $computer -Properties LastLogonDate
$ping = Test-Connection -quiet -computername $computer
New-Object -TypeName PSObject -Property @{
Computername = $computer
OnlineStatus = $ping
LoggedIn = $model.username
OSVersion = $os.Caption
LastLogonDate = $AD.LastLogonDate
}
}
$Collection | Export-Csv TestADComputers.csv -NoTypeInformation
答案 1 :(得分:1)
其他答案已经注意到foreach
循环并未完全包围所有必需的代码。另一个选择是取消函数中的循环并允许函数处理管道输入。这将使您更灵活地使用该功能。
function Get-loginTest {
[CmdLetBinding()]
param (
[Parameter(ValueFromPipeline=$True)]
$computer = 'localhost'
)
process {
$os = Get-WmiObject Win32_OperatingSystem -computer $computer
$bios = Get-WmiObject Win32_BIOS -computer $computer
$model = Get-WmiObject Win32_ComputerSystem -computer $computer
$AD = Get-ADComputer $computer -Properties LastLogonDate
$ping = Test-Connection -quiet -computername $computer
$obj = New-Object -TypeName PSObject
$obj| Add-Member -MemberType NoteProperty -Name Computername -Value $computer
$obj| Add-Member -MemberType NoteProperty -Name OnlineStatus -value $ping
$obj| Add-Member -MemberType NoteProperty -Name LoggedIn -Value ($model.username)
$obj| Add-Member -MemberType NoteProperty -Name OSVersion -Value ($os.Caption)
$obj| Add-Member -MemberType NoteProperty -Name LastLogonDate -Value ($AD.LastLogonDate)
$obj
}
}
这将允许您从Get-Content
输入计算机:
get-content -path 'c:\powershell\computernames.txt'|
Get-loginTest|
Export-Csv C:\temp\TestADcomputers.csv
或者您可以为该功能指定一台计算机。
Get-loginTest workstation1|
Export-Csv C:\temp\TestADcomputers.csv
请注意,我还删除了Get-Content
调用作为计算机名称的默认值。依赖外部数据源通常不是默认值的好主意。使用localhost
代替将使该功能更具可移植性/可重用性。
答案 2 :(得分:0)
function Get-loginTest {
#[CmdletBinding()]
param (
$computername = (get-content -path 'c:\powershell\computernames.txt')
)
[string]$ErrorLog = 'c:\powershell\useful\errorlog\retry.txt',
[switch]$LogErrors
$objs = @()
foreach ($computer in $computername)
{
$os = Get-WmiObject Win32_OperatingSystem -computer $computer
$bios = Get-WmiObject Win32_BIOS -computer $computer
$model = Get-WmiObject Win32_ComputerSystem -computer $computer
$AD = Get-ADComputer $computer -Properties LastLogonDate
$ping = Test-Connection -quiet -computername $computer
$obj = New-Object -TypeName PSObject
$obj| Add-Member -MemberType NoteProperty -Name Computername -Value $computer
$obj| Add-Member -MemberType NoteProperty -Name OnlineStatus -value $ping
$obj| Add-Member -MemberType NoteProperty -Name LoggedIn -Value ($model.username)
$obj| Add-Member -MemberType NoteProperty -Name OSVersion -Value ($os.Caption)
$obj| Add-Member -MemberType NoteProperty -Name LastLogonDate -Value ($AD.LastLogonDate)
$objs += $obj
}
Write-Output $objs
}
Get-loginTest | Export-Csv C:\PowerShell\TestADcomputers.csv -NoTypeInformation
创建一个数组来存储每个自定义对象,将每个自定义对象添加到for循环中的数组