Domanda

I have 2 computer names in the text file in this line - get-content -path 'c:\powershell\computernames.txt'. However, it only exports one computername into 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
È stato utile?

Soluzione

Simple enough, you are only telling it to output 1 object. That object was made after your ForEach loop, so it's only seeing the results of the last cycle of the loop. What you want to do is move the object creation inside your ForEach loop, and have an array that contains those objects. Then once your ForEach loop finishes you can output that to the CSV. This slightly modified script should do what you want:

#[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

Altri suggerimenti

The other answers have noted that the foreach loop did not completely surround all of the necessary code. Another option is to do away with the loop in the function and allow the function to process pipeline input. This will give you more flexibility on how you use the function.

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
    }
}

This will allow you to pipe the computers in from Get-Content:

get-content -path 'c:\powershell\computernames.txt'|
    Get-loginTest| 
    Export-Csv C:\temp\TestADcomputers.csv

Or you can specify a single computer for the function.

Get-loginTest workstation1| 
    Export-Csv C:\temp\TestADcomputers.csv

Note that I also removed the Get-Content call as the default value for the computer name. Relying on an external data source is not usually a good idea for a default value. Using localhost instead will make the function more portable/reusable.

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

Create an array to store each custom object in, add each custom object to the array in your for loop

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top