Export-csv And Foreach issue

Guys.

I have two issues with a script I am working on.

1)The script will obtain the Computer name, username and last reboot time. I am pulling a list of computer names from Active Directory. Now if there is a single computer object in the OU then this returns all the info that I am expecting to return. If I put two computers in the OU then its only returning the info for the first machine and not the second.

I would have though this would have worked as I am using ForEach-Object ?

2) I am struggling with the output and would like to export it to CSV , I receive the error below. using write-object $object does display correctly.

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value

The code I have so far is below

====================

$ADComputers = Get-QADComputer -SizeLimit 0 -SearchRoot "OU NAME HERE" |


ForEach-Object {
$LoggedOnUser = Get-WmiObject win32_computersystem -Computer $_.name | Select username
$RebootTime = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_.name | Select-Object  __SERVER,@{label='LastBootUpTime';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}
$Proc = Get-WmiObject Win32_processor -ComputerName $_.name | Select-Object -First 1

$Object = New-Object PSObject -Property @{

ComputerName           = $proc.SystemName
LoggedOnUser           = $LoggedOnUser.username
RebootTime             = $RebootTime.LastBootUpTime
}
}

write-output $Object

July 8th, 2015 8:48am

your object should be outside the loop, since you're re-writing it and that's why you see just the one machine.

it should be an array and you should add to it within the loop.

other option is to do the write-output within the loop, if you're not reusing the object.

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 9:18am

Try this way - 

Get-QADComputer -SizeLimit 0 -SearchRoot "OU NAME HERE" |
ForEach-Object {
$LoggedOnUser = Get-WmiObject win32_computersystem -Computer $_.name | Select username
$RebootTime = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_.name | Select-Object  __SERVER,@{label='LastBootUpTime';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}
$Proc = Get-WmiObject Win32_processor -ComputerName $_.name | Select-Object -First 1

New-Object PSObject -Property @{

ComputerName           = $proc.SystemName
LoggedOnUser           = $LoggedOnUser.username
RebootTime             = $RebootTime.LastBootUpTime
}

}| export-csv C:\test1.csv -notype

July 8th, 2015 9:20am

$Object is overwritten on each iteration of the loop, so you are getting the last object. You will need to move the write-Output $Object to be the last statement inside the loop.

If you want to export everything to csv, you will need to add the $object to an array object, then after the loop, pipe that array object to export-csv

Also I do not see any Export-Csv commands, so not sure what you are doing but based on the error, it looks like you are setting the delimeter property? If so you need to do it like, Export-Csv -Delimet

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 9:21am

Thanks for the reply guys.

So on running the code from Braham20 I get the message below.

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that

is not null or empty and then try the command again.


If I start to debug this. I can see that the $_.name is showing as DOMAIN\Computername$

Would / should this not just be the NetBIOS name with no domain prefix?

July 8th, 2015 9:30am

Did you change the OU string for the SearchRoot
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 9:36am

Hello.

Yes I changed the OU string so that it is pointing to the correct location. I have copied exactly what Braham20 provided and changed the OU.

Craig

July 8th, 2015 10:09am

I have repeatedly posted the correct answer in your other threads.  You either change it incorrectly or ignore the answer.  You need to stop asking questions and take some time to try to understand how this is working.  Ask yourself why you cannot see how it is working.  Solve that problem and you will have learned a huge amount about how PowerShell works.
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 10:10am

Here it is once again:

Get-QADComputer -SizeLimit 0 -SearchRoot "OU NAME HERE" |
    ForEach-Object {
        $comp = Get-WmiObject win32_computersystem -Computer $_.name
        $OS = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_.name 
        New-Object PSObject -Property @{    
            ComputerName = $_.Name
            LoggedOnUser = $comp.username
            RebootTime = $OS.ConvertToDateTime($OS.LastBootUpTime
        }
    
    } | 
    Export-Csv C:\test1.csv -notype

This time don't add things blindly.  This are part of the cause of your confusion.

Learn to format code correctly so you can more easily see the mistakes.

July 8th, 2015 10:15am

Dude ffs! I have been learning but surely a forum is a place to ask questions?? No? I got my script working off my own bat this morning and not from anything that you have provided. However I then run in to a new problem which I am now asking for a little assistance on.  

I am trying to work this out but asking for a little help.

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 10:17am

jvr - Thank you for the code you just provided. I will be looking at what you have provided and what I had before to see where the issue is / was.

You code does work but the reboot time displays

System.Collections.Hashtable

So that's something I need to research.

July 8th, 2015 10:22am

If I change
RebootTime =  @{N='LastBootUpTime';E={ $OS.ConvertToDateTime($OS.LastBootUpTime)}}

To

RebootTime = $RebootTime.LastBootUpTime

This displays the reboot time correctly in the CSV 

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 10:34am

If I change
RebootTime =  @{N='LastBootUpTime';E={ $OS.ConvertToDateTime($OS.LastBootUpTime)}}

To

RebootTime = $RebootTime.LastBootUpTime

This displays the reboot time correctly in the CSV 

You are still missing the whole point and not understanding what is happening:

Try this at a prompt.

PS C:\scripts> $OS = Get-WmiObject -class Win32_OperatingSystem
PS C:\scripts> $os.LastBootUpTime
20150611135809.626398-240
PS C:\scripts> $os.ConvertToDateTime($os.LastBootUpTime)

Thursday, June 11, 2015 1:58:09 PM

July 8th, 2015 11:33am

Sorry I forgot to strip off your unneeded bits:

Get-QADComputer -SizeLimit 0 -SearchRoot "OU NAME HERE" |
    ForEach-Object {
        $comp = Get-WmiObject win32_computersystem -Computer $_.name
        $OS = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_.name 
        New-Object PSObject -Property @{    
            ComputerName = $_.Name
            LoggedOnUser = $comp.username
            RebootTime = $OS.ConvertToDateTime($OS.LastBootUpTime
        }
    
    } | 
    Export-Csv C:\test1.csv -notype
I fixed the one above.
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 11:35am

Thanks for the reply guys.

So on running the code from Braham20 I get the message below.

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that

is not null or empty and then try the command again.


If I start to debug this. I can see that the $_.name is showing as DOMAIN\Computername$

Would / should this not just be the NetBIOS name with no domain prefix?

  • Edited by WallaceTech Wednesday, July 08, 2015 1:33 PM
July 8th, 2015 1:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics