Simple script to write to file from multiple hosts

here's a stumper. At least for me. Take the following script and run it from local host. I creates a .csv file just fine with the two parameters given (hostname and date). Simple right, well....... yes and no runs fine on local machine and does what it's suppose to. Run it from a remote host and it only writes the date. No hostname. OK to confuse the issue more. Delete the file and run the same script and it will create the file with the correct items (hostname and date). Run the very same script from local machine and it will only add the date, no hostname.

        $date = Get-Date -format MMM.dd.yyyy
      $compname = get-content env:computername
      $tag = "$date" , "$compname"
      $psObject = $null
      $psObject = New-Object psobject
      foreach($o in $tag)
            {add-Member -InputObject $psobject -Membertype noteproperty -Name $o -Value $o}
      $psObject | Export-Csv -NoType -Append -Path "\\wit2\temp\tag.csv" -Force

What majic am I missing.

Thanks. Richard.
RichardPWolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
You want the property names to be consistent.  It doesn't make sense to have a property called "mycomputer" when the value of that property is just going to be the same as the name.  Same for a property called "Apr.14.2015".  The properties should have a consistent name, whether they are run one computer or another, today or some other day.

When you use the -force (Luke :) ) parameter, you can append items that have matching property names, but it won't create new columns for the mismatched properties.

$date = Get-Date -format MMM.dd.yyyy
$compname = get-content env:computername
$psObject = New-Object psobject
Add-Member -InputObject $psobject -Membertype noteproperty -Name "Date" -Value $date
Add-Member -InputObject $psobject -Membertype noteproperty -Name "Computer" -Value $compname
$psObject | Export-Csv -NoType -Append -Path "\\wit2\temp\tag.csv"

Open in new window


An optimized version.
New-Object PsObject -Property @{ `
                        Date = Get-Date -format MMM.dd.yyyy
                        Computer = $env:computername
                        } |
 Export-Csv -NoType -Append -Path "\\wit2\temp\tag.csv"

Open in new window

RichardPWolfAuthor Commented:
Wow. seems much simpler. I'll give it a try. Just for background what I'm trying to do is create a file that when an event is triggered on any one of about 20 servers it will write that basic information into a file. At the end of the month I'll be doing a count object to report how many times each server had that event happen. Initially I was trying to report using winevent etc. but it was becoming to un-wielding so I simplified the process.

Thanks. I'll get back to you on this as I'm trying to get a router working at this time.
RichardPWolfAuthor Commented:
Haven't forgot. Been battling power issues and hardware failures. Should be able to test next week.

Thanks for the patience.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

RichardPWolfAuthor Commented:
Sorry it took so long (priorities).

I made a minor change to your script to write to a network share but I receive this message when executing the script from a remote system. It works fine on my workstation. The only difference that I can see is possibly versions of powershell.

---------error------------
Export-Csv : A parameter cannot be found that matches parameter name 'Append'.
At line:5 char:31
+     Export-Csv -NoType -Append <<<<  -Path "\\jill\IT-Dept\Docs\Docs\Monthly IT Reports\EventLogBackup\tag.csv"
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand
-----------end of error--------------
footechCommented:
The -Append parameter was added to Export-CSV in PS 3.0.  If that's not available where you need to run it and can't upgrade, then your other option is to use Add-Content instead of Export-CSV.  Instead of submitting complex objects (like with Export-CSV), you will have to manually construct a string with the format you want to submit to Add-Content.

$Date = Get-Date -format MMM.dd.yyyy
$Computer = $env:computername
Write-Output "$Date,$Computer" | Add-Content -Path "\\wit2\temp\tag.csv"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RichardPWolfAuthor Commented:
OK, that's what I thought. I shouldn't have any problem upgrading and will do that as soon as possible. Of course your second script looks interesting and simple.
RichardPWolfAuthor Commented:
Thank you. I know this was extremely simple and after seeing the resolution I'm banging my head against the desk for not seeing it. I've been staring a code too much lately:)

Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.