none
How to Gather Machine Info Export-csv RRS feed

  • Question

  • Hello, I have one script to gather information about files I'm searching for, then outputs the info. The script works well. But now that I've found the information (file size) I need to replace the file and report about the results. This replace script works, but how would I go about Export-csv the machine name and path where the file was found and whether or not it was successfully replaced or not? I might even want to show the file length. I believe the $env:computername can be used, but it doesn't seem to work if I use that instead of echo? Thanks.

    $FileName = "\\firm\dfs\AppMedia\eMailSignature\logo.png"
    $TargetDir = "c:\Users\*\AppData\Roaming\Microsoft\Signatures\"

    $FilesToOverride = get-childitem $TargetDir -recurse | where {$_.length -eq 6485}

    Foreach ($file in $FilesToOverride) 
    {
        echo ("Replacing " + $file.FullName + " with " + $FileName)
        copy-item $FileName ($file.DirectoryName + "\" + $file.Name) 
    }

    Wednesday, May 28, 2014 5:00 PM

Answers

  • Maybe try it like this:

    $LogResults = "\\server\share\file.csv"
    $FileName = "\\firm\dfs\AppMedia\eMailSignature\logo.png"
    $TargetDir = "c:\Users\*\AppData\Roaming\Microsoft\Signatures\"
    
    $FilesToOverride = get-childitem $TargetDir -recurse | where {$_.length -eq 6485}
    
    $Results = Foreach ($file in $FilesToOverride) {
        $Result = "" | Select Computer,File,Result
        $Result.Computer = $env:COMPUTERNAME
        $Result.File = $File.FullName
        Try {
           copy-item $FileName $file.FullName -Force
           $Result.Result = "SUCCESS"
        } Catch {
           $Result.Result = "FAILED - $($Error[0].Exception.Message)"
        }
        $Result
    }
    $Results | Select Computer,File,Result | Export-CSV $LogResults -Append -NoType
    

    I'm assuming you're running this script on target computers and you want them all to output to a common file, which is why I specified a UNC path to the output file.  You need to make sure the context in which the script runs has write access to that output file. 


    I hope this post has helped!

    • Marked as answer by MarcGel Wednesday, May 28, 2014 8:44 PM
    Wednesday, May 28, 2014 6:54 PM

All replies

  • Maybe try it like this:

    $LogResults = "\\server\share\file.csv"
    $FileName = "\\firm\dfs\AppMedia\eMailSignature\logo.png"
    $TargetDir = "c:\Users\*\AppData\Roaming\Microsoft\Signatures\"
    
    $FilesToOverride = get-childitem $TargetDir -recurse | where {$_.length -eq 6485}
    
    $Results = Foreach ($file in $FilesToOverride) {
        $Result = "" | Select Computer,File,Result
        $Result.Computer = $env:COMPUTERNAME
        $Result.File = $File.FullName
        Try {
           copy-item $FileName $file.FullName -Force
           $Result.Result = "SUCCESS"
        } Catch {
           $Result.Result = "FAILED - $($Error[0].Exception.Message)"
        }
        $Result
    }
    $Results | Select Computer,File,Result | Export-CSV $LogResults -Append -NoType
    

    I'm assuming you're running this script on target computers and you want them all to output to a common file, which is why I specified a UNC path to the output file.  You need to make sure the context in which the script runs has write access to that output file. 


    I hope this post has helped!

    • Marked as answer by MarcGel Wednesday, May 28, 2014 8:44 PM
    Wednesday, May 28, 2014 6:54 PM
  • Use PowerShell  - Don't use any substitutes.

    $FileName="\\firm\dfs\AppMedia\eMailSignature\logo.png"
    $TargetDir="c:\Users\*\AppData\Roaming\Microsoft\Signatures\"
    get-childitem $TargetDir -recurse | 
         where {$_.length -eq 6485} |
         ForEach-Object{
                     Write-Host ('Replacing {0} with {1}' -f $_.FullName, $FileName) -fore green
                     copy-item $FileName $_
                     New-Object PsObject -Property @{ComputerName=$env:COMPUTERNAME;Fullname=$_.FileName}
         }


    Don't write code that you don't have to write and don't use old PowerShell V1 structures.


    ¯\_(ツ)_/¯



    • Edited by jrv Wednesday, May 28, 2014 7:14 PM
    Wednesday, May 28, 2014 7:13 PM
  • Works well. Thanks! Question though, I'm running this script and a copy of it with a different file (same concept though) through the Altiris Deployment solution. It's like running a powershell script from a batch file. It looks like the output only happened on one machine and not the 4 in my test group. I tried the -NoClobber switch but that didn't seem to work. Only my machine appears as output in the .csv. 
    Wednesday, May 28, 2014 8:03 PM
  • If you are trying to use this with Altiris then you will have to post your issues to the Altiris forum as was noted before. It is not likely that you can write to a remote share from a remote script.


    ¯\_(ツ)_/¯

    Wednesday, May 28, 2014 8:19 PM
  • I don't know Altiris that well, but with SCCM the computer's local service account is effectively the 'user' that would be running a deployed script.  So if that account doesn't have privileges to the server share, the file can't be updated.  I believe if you granted the Domain Computers group write access to the folder/file it might solve this issue.

    I hope this post has helped!

    Wednesday, May 28, 2014 8:49 PM