none
Function output getting overwritten RRS feed

  • Question

  • I am trying to output the following function I built to a csv file. the function works, except that it overwrites instead of append. If I append to a txt file, formatting is confusing because it puts a header for every row of data. How would I output an all the data to a csv with a single header after loop is completed?

            FUNCTION SCCM-Getuseraffinity {
        Param([parameter(Mandatory = $true)]$SamAccountName,
        $SiteName="Site",
        $SCCMServer="server.domain.com")
        $SCCMNameSpace="root\sms\site_$SiteName"
    
        $userinfo=Get-WmiObject -namespace $SCCMNameSpace -computer $SCCMServer     -query "select name from sms_r_system where LastLogonUserName='$samaccountname'"
        $workstation=Get-WmiObject -class sms_g_system_computer_system -namespace $SCCMNameSpace -computer $SCCMServer -Filter "Name = '$($userinfo.Name)'"
        $Query = "Select Version,CSDVersion,SystemDirectory,Installdate,LastBootuptime,installdate,caption,Description from SMS_G_System_OPERATING_SYSTEM JOIN SMS_R_System ON SMS_R_System.ResourceID = SMS_G_System_OPERATING_SYSTEM.ResourceID where SMS_R_System.NetbiosName='$($userinfo.Name)'"
        $osinfo= Get-WmiObject -Query $Query -ComputerName $SCCMServer -Namespace root/SMS/Site_$SiteName
    
    
    
     [pscustomobject]@{
                                                User=$workstation.username
                                                ComputerName=$userinfo.Name;
                                                OS = $OSInfo.caption;
     }}
    
    
    $users=get-content 'c:\temp\users.txt'
    foreach($user in $users){
    
    SCCM-Getuseraffinity -SamAccountName $user  | export-csv 'c:\temp\exchange_cachedmode_group_check.csv' }
    

    Tuesday, June 27, 2017 4:51 PM

Answers

  • You should read the help for the cmdlet Export-CSV completely and very carefully! ... including the examples ...  especially about the parameter -Append!!

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by glacket Tuesday, June 27, 2017 7:57 PM
    Tuesday, June 27, 2017 5:31 PM

All replies

  • You should read the help for the cmdlet Export-CSV completely and very carefully! ... including the examples ...  especially about the parameter -Append!!

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by glacket Tuesday, June 27, 2017 7:57 PM
    Tuesday, June 27, 2017 5:31 PM
  • That worked. I have used -append argument with "export-csv" without much success in the past and didn't even think of trying it this time. Thanks!
    Tuesday, June 27, 2017 7:57 PM