locked
Missing computername in output of script RRS feed

  • Question

  • My goal is to produce a quick report that the techs can use to find inconsistencies involving Adobe Flash and other installs. I have the following code and it works as far as giving the information, but I am missing the computer name. I don't think I am understanding or not using the right syntax for both pieces of information to display.

    $cn = Import-Csv -Path "c:\ps\computerlist.csv"
    
    ForEach-Object -Process {
    	Get-ChildItem C:\Windows\System32\Macromed\Flash | select VersionInfo | Format-List
    	}
    I am using v4.0 on a Windows 7 machine. The clients are all Windows 7 with defaults. Do I need to place an array in the code and then pass the get-childitem statement?

    Friday, April 4, 2014 5:22 PM

Answers

  • Here's an example you can build from (using the straight text file of computer names):

    Get-Content .\computerList.txt | ForEach {
    
        $PCName = $_
        
        If ( Test-Path "\\$PCName\C$\Windows\System32\Macromed\Flash") {
        
            $flashVer = Get-ChildItem "\\$PCName\C$\Windows\System32\Macromed\Flash\*.ocx"
            
            $props = @{
                ComputerName = $PCName
                FlashVersion = $flashVer.VersionInfo.FileVersion
            }
    
        }
    
        Else {
        
            $props = @{
                ComputerName = $PCName
                FlashVersion = 'Not Found'
            }
    
        }
    
        New-Object PsObject -Property $props
    
    } | Export-Csv .\flashVersionCheck.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:28 PM

All replies

  • Hi,

    What are you planning on doing with $cn? I don't see you using it anywhere in your script.

    Are you planning on running this script against remote machines or only locally?


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 5:40 PM
  • All of this will be remote. I stuck the $cn variable in the select statement but it returned an error. Would this be a better approach?

    $cn = Import-Csv -Path "c:\ps\computerlist.csv"
    
    ForEach-Object -Process {
    	
    ($_.name in $cn) {Get-ChildItem C:\Windows\System32\Macromed\Flash | select VersionInfo | Format-List}
    	}

    Friday, April 4, 2014 5:59 PM
  • No, not really. You're still looking at a local path, so you'll never get any results other than what's on the local PC.

    What's in your CSV file?


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:03 PM
  • Computer names only. I can tell that the above didn't work, so should in explore Invoke-Command instead?
    Friday, April 4, 2014 6:06 PM
  • If you have remoting enabled, then Invoke-Command will work for you. If not, stick with UNC admin shares.

    Based on your previous post, the header in the CSV is Name, correct?


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:11 PM
  • Computer names only. I can tell that the above didn't work, so should in explore Invoke-Command instead?

    Yes, you should explore Invoke-Command if you want to use local paths (since Invoke-Command runs as if you're on the remote computer). Either that, or your paths should be UNCs, such as Get-ChildItem -Path "\\$cn\C$\Windows\System32\Macromed\Flash". If you use the second option (UNCs), then you might consider wrapping your Get-ChildItem cmdlet inside of an If statement that first checks for the path (Test-Path cmdlet).

    Edit: Cmdlet typo.

    Friday, April 4, 2014 6:17 PM
  • Actually there isn't a header. Should I have one?
    Friday, April 4, 2014 6:17 PM
  • Actually there isn't a header. Should I have one?

    Yep. Alternatively, you can just rename the file to whatever.txt and use Get-Content instead of Import-Csv.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:19 PM
  • Thanks for the guidance. I will explore that a bit more and post later what works.
    Friday, April 4, 2014 6:21 PM
  • Here's an example you can build from (using the straight text file of computer names):

    Get-Content .\computerList.txt | ForEach {
    
        $PCName = $_
        
        If ( Test-Path "\\$PCName\C$\Windows\System32\Macromed\Flash") {
        
            $flashVer = Get-ChildItem "\\$PCName\C$\Windows\System32\Macromed\Flash\*.ocx"
            
            $props = @{
                ComputerName = $PCName
                FlashVersion = $flashVer.VersionInfo.FileVersion
            }
    
        }
    
        Else {
        
            $props = @{
                ComputerName = $PCName
                FlashVersion = 'Not Found'
            }
    
        }
    
        New-Object PsObject -Property $props
    
    } | Export-Csv .\flashVersionCheck.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:28 PM
  • So here we are replacing the ForEach-Object with just ForEach, and we are specifying a variable for the computer objects. I think I understand what you have presented. I didn't think to approach it this way. I appreciate the help.
    Friday, April 4, 2014 6:37 PM
  • Cheers, you're welcome.

    Just a clarification, I'm still using ForEach-Object, I'm just leaving the -Object part off. You could also skip assigning the pipeline variable to a named variable, I only do it because I think it makes the code a bit more readable for others.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 6:41 PM
  • I understand the changes, but when I run it this way I get a blank file. If I run the Get-ChildItem locally and alone, I get the file name property and this includes the version number. Am I selecting the right property?

    Get-ChildItem "c:\Windows\System32\Macromed\Flash\*.ocx"  | Format-List
    
    
    
    Directory: C:\Windows\System32\Macromed\Flash
    
    
    
    Name           : Flash32_12_0_0_77.ocx
    Length         : 16350088
    CreationTime   : 4/1/2014 8:44:47 AM
    LastWriteTime  : 4/1/2014 8:44:47 AM
    LastAccessTime : 4/1/2014 8:44:47 AM
    VersionInfo    : File:             C:\Windows\System32\Macromed\Flash\Flash32_12_0_0_77.ocx
                     InternalName:     Adobe Flash Player 12.0
                     OriginalFilename: Flash.ocx
                     FileVersion:      12,0,0,77
                     FileDescription:  Adobe Flash Player 12.0 r0
                     Product:          Shockwave Flash
                     ProductVersion:   12,0,0,77
                     Debug:            False
                     Patched:          False
                     PreRelease:       False
                     PrivateBuild:     False
                     SpecialBuild:     False
                     Language:         English (United States)
    

    I tried changing this line to .Name:

    FlashVersion $flashver.Name

    but the ouptut is still blank.

    Friday, April 4, 2014 7:08 PM
  • Are you sure you can remotely access these machines using the admin share? I ran the code as pasted before posting and it worked just fine (targeting machines with and without Flash installed).

    Also, I was specifically only returning the FileVersion property of the VersionInfo collection ($flashVer.VersionInfo.FileVersion).


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 7:26 PM
  • I ran Mike's code and it worked fine.

    I would start your troubleshooting by removing the  | Export-Csv and then replacing your Get-Content Computerlist.txt and instead put in a name of a known good computer and run the script to view its output. Adding some verbose output would also help out as well.

    Basically something like this:

    "Computername" | ForEach {
        $PCName = $_    
        Write-Verbose "Checking $PCName" -Verbose
        If ( Test-Path "\\$PCName\C$\Windows\System32\Macromed\Flash") { 
            Write-Verbose "Folder path is good!" -Verbose  
            $flashVer = Get-ChildItem "\\$PCName\C$\Windows\System32\Macromed\Flash\*.ocx"        
            $props = @{
                ComputerName = $PCName
                FlashVersion = $flashVer.VersionInfo.FileVersion
            }
        }
        Else {    
            Write-Verbose "Folder not found!" -Verbose
            $props = @{
                ComputerName = $PCName
                FlashVersion = 'Not Found'
            }
        }
        New-Object PsObject -Property $props
    }


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book

    Friday, April 4, 2014 7:28 PM
  • When I run this alone:

    Get-Content -Path "c:\PS\Text\ROList.txt"

    I get the returned machine names. All are online and should be accessible for and remote PS is enabled by policy.

    Friday, April 4, 2014 7:30 PM
  • Does your account actually have rights though? Just reading your input text file isn't going to verify that you can connect to them.

    Simple test - open up Explorer and type in \\SomeRemoteComputerNameInYourList\c$ and see if you can browse the filesystem.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 7:41 PM
  • Does your account actually have rights though? Just reading your input text file isn't going to verify that you can connect to them.

    Simple test - open up Explorer and type in \\SomeRemoteComputerNameInYourList\c$ and see if you can browse the filesystem.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)


    To add to this, are you seeing any errors at all when running the code? What is $ErrorActionPreference set to? If set to SilentlyContinue, then I would recommend changing it to Continue so you can see any errors that pop in.

    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book

    Friday, April 4, 2014 7:44 PM
  • Sorry for the delay, I was called away. I just double checked and I cannot get to these machines. Something I will have to have the techs onsite remedy. I appreciate all the assistance
    Friday, April 4, 2014 8:35 PM
  • I have the rights, but earlier in the week we have had issues in the domain with local firewalls. The techs should have this cleared up soon. I tested again on three machines here in the office and I had one typo and one missing bracket. Otherwise no errors. I am getting the computers to respond now.

    So I understand completely, when you precede a { with @ like so @{}, you are effectively creating an array on the fly from the input objects. Do I have that right?

    Friday, April 4, 2014 8:49 PM
  • So I understand completely, when you precede a { with @ like so @{}, you are effectively creating an array on the fly from the input objects. Do I have that right?

    The @{} notation is used to create a hash table:

    http://ss64.com/ps/syntax-hash-tables.html

    They're quite useful once you get the hang of them.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 8:52 PM
  • I sure appreciate the assistance. I have learned some new things about PowerShell working through this. When I corrected my mistakes, and ran on some known good paths, it worked beautiful. You have helped me see the whole approach in a much better method than I was taking. My method worked fine local and most likely for a single machine at a time, but this works much better. Thanks again.
    Friday, April 4, 2014 8:56 PM
  • Cheers, you're very welcome.

    Welcome to the fun world of PowerShell.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Friday, April 4, 2014 8:58 PM