Powershell script help required please

Hi all, i have the following script which i think im almost there. what im looking to do is ping the computer in the txt file to see if it responds, if so it seraches the drives for mp3's. also if like to add if the scan comes back with no results as in which computer. is this possible?

thanks

$computerName = get-content "C:\computers.txt"
 
foreach ($computer in $computerName) {
 
        if (test-Connection -ComputerName $computer -Count 1 -Quiet ) {  
         
            write-Host "$computer is alive and Pinging " -ForegroundColor Green
         
                    } else
                     
                    { Write-Warning "$Server seems dead not pinging"

$strComputers = Get-Content -Path "C:\computers.txt"
[bool]$firstOutput = $true
foreach($strComputer in $strComputers)
{  
$colFiles = Get-Wmiobject -namespace "root\CIMV2" `
-computername $strComputer `
-Query "Select * from CIM_DataFile `
Where Extension = 'mp3'" -ErrorAction SilentlyContinue
foreach ($objFile in $colFiles)
{

if($objFile.FileName -ne $null)
{
$filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." `
+ $objFile.Extension;
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" `
+ $filepath `
+ "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"

$colOwners = Get-Wmiobject -namespace "root\CIMV2" `
-computername $strComputer `
-Query $query -ErrorAction SilentlyContinue
$objOwner = $colOwners[0]
$user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
$output = $strComputer + "," + $filepath + "," + $user + "," + $objFile.FileSize/1MB + "," + $objFile.LastModified
if($firstOutput)
{
Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv"
$firstOutput = $false
}
else
{
Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv" -append
}
}
}
}
}
}
LVL 15
cwstad2Asked:
Who is Participating?
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.

David Johnson, CD, MVPOwnerCommented:
$computerName = get-content "C:\computers.txt"
 foreach ($computer in $computerName) {
         if (test-Connection -ComputerName $computer -Count 1 -Quiet ) {  
             write-Host "$computer is alive and Pinging " -ForegroundColor Green
                     } 
         else
             { 
             Write-Warning "$Server seems dead not pinging"
             }
#
# why do it 2 times
#
$strComputers = Get-Content -Path "C:\computers.txt"
[bool]$firstOutput = $true
foreach($strComputer in $strComputers){  
    $colFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $strComputer -Query "Select * from CIM_DataFile Where Extension = 'mp3'" -ErrorAction SilentlyContinue
    foreach ($objFile in $colFiles)
        {
        if($objFile.FileName -ne $null)
            {
            $filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." + $objFile.Extension;
            $query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" + $filepath + "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
            $colOwners = Get-Wmiobject -namespace "root\CIMV2" -computername $strComputer -Query $query -ErrorAction SilentlyContinue
            $objOwner = $colOwners[0]
            $user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
            $output = $strComputer + "," + $filepath + "," + $user + "," + $objFile.FileSize/1MB + "," + $objFile.LastModified
          
            if($firstOutput)
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv"
                $firstOutput = $false
                }
            else
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv" -append
                }
        }
    }
    }
}

Open in new window

reformatted and put into a code block
0
cwstad2Author Commented:
Hi David the first part of the script is so that I can see that the pc is on the network. What the intention of the script is to ping then scan the pc for mp3 or return no ping.  In addition the element I'm missing is to write in the file the name of the computer which has returned no results.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This is the simplified and expanded script. Note you will not get a file entry if the remote is not reachable, it is only displayed on the screen. If you want to change that, I recommend to add another column to the CSV, so we can properly indicate.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

David Johnson, CD, MVPOwnerCommented:
then don't do the get-content 2x if computer responds from a ping call a function to search the drives
$computerName = get-content "C:\computers.txt"
 foreach ($computer in $computerName) {
         if (test-Connection -ComputerName $computer -Count 1 -Quiet ) {  
             write-Host "$computer is alive and Pinging " -ForegroundColor Green
           computeralive $computerName
           }
         else
             { 
             Write-Warning "$Server seems dead not pinging"
             }

function compoutealive 
{
$strComputer = $computername
[bool]$firstOutput = $true

    $colFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $strComputer -Query "Select * from CIM_DataFile Where Extension = 'mp3'" -ErrorAction SilentlyContinue
    foreach ($objFile in $colFiles)
        {
        if($objFile.FileName -ne $null)
            {
            $filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." + $objFile.Extension;
            $query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" + $filepath + "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
            $colOwners = Get-Wmiobject -namespace "root\CIMV2" -computername $strComputer -Query $query -ErrorAction SilentlyContinue
            $objOwner = $colOwners[0]
            $user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
            $output = $strComputer + "," + $filepath + "," + $user + "," + $objFile.FileSize/1MB + "," + $objFile.LastModified
          
            if($firstOutput)
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv"
                $firstOutput = $false
                }
            else
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv" -append
                }
        }
    }
    }
}

Open in new window

0
cwstad2Author Commented:
Hi Qlemo, i dont see the script. Thanks for your assistance
0
David Johnson, CD, MVPOwnerCommented:
function Computer-Alive 
{
 write-Host "Checking $computer" -ForegroundColor Green
[bool]$firstOutput = $true

    $colFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $computer -Query "Select * from CIM_DataFile Where Extension = 'mp3'" -ErrorAction SilentlyContinue
    foreach ($objFile in $colFiles)
        {
        if($objFile.FileName -ne $null)
            {
            $filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." + $objFile.Extension;
            $query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" + $filepath + "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
            $colOwners = Get-Wmiobject -namespace "root\CIMV2" -computername $computername -Query $query -ErrorAction SilentlyContinue
            $objOwner = $colOwners[0]
            $user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
            $output = $computername + "," + $filepath + "," + $user + "," + $objFile.FileSize/1MB + "," + $objFile.LastModified
          
            if($firstOutput)
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv"
                $firstOutput = $false
                }
            else
                {
                Write-output $output | Out-File -Encoding ascii -filepath "C:\mp3.csv" -append
                }
        }
    }
    }


$computerName = get-content "C:\computers.txt"
#$computername =  'localhost','localhost','localhost'
 foreach ($computer in $computerName) {
         if (test-Connection -ComputerName $computer -Count 1 -Quiet ) {  
             write-Host "$computer is alive and Pinging " -ForegroundColor Green
             Computer-Alive $computer
           }
         else
             { 
             Write-Warning "$Server seems dead not pinging"
             }
}

Open in new window

0
cwstad2Author Commented:
Thanks David, do you think it would be possible to write into the file the name of the computer and satus nothing found for example
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Damned, I already dismissed my temp data, so had to rewrite it all :/.
Remove-Item C:\mp3.csv
foreach ($computer in get-content "C:\computers.txt") {
  if (!test-Connection -ComputerName $computer -Count 1 -Quiet ) {
    Write-Warning "$Server seems dead not pinging"
    "$Computer,*Dead?*,,," | Out-File C:\mp3.csv -Encoding ASCII -Append
  } else { 
    write-Host "$computer is alive and Pinging " -ForegroundColor Green
    [bool] $mp3Found = $false
    $colFiles = Get-Wmiobject -computername $Computer -Query "Select * from CIM_DataFile Where Extension = 'mp3'" -ErrorAction SilentlyContinue |
      ? { $_.FileName } |
      % {
        $filepath = $_.Drive + $_.Path + $_.FileName + '.' + $_.Extension
        $query    = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='$filepath'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
        $objOwner = Get-Wmiobject -computername $Computer -Query $query -ErrorAction SilentlyContinue | Select -First 1
        $user     = $objOwner.ReferencedDomainName + '\' + $objOwner.AccountName
        "$Computer,$filepath,$user,$($objFile.FileSize/1MB),$($objFile.LastModified)" | Out-File C:\mp3.csv -Encoding ASCII -Append
      }
    if (!$mp3Found) { "$Computer,*No files*,,," | Out-File C:\mp3.csv -Encoding ASCII -Append }
  }
}

Open in new window

This now writes a status in the filename column if no files have been found or the computer did not respond to ping.
0
cwstad2Author Commented:
Hi Qlemo, thanks for the reply. I seem to have an issue with parts of the script. It returns ping results as active from machines that are off the network and also says no files found on test machines i know that have mp3s. Thanks for your assistance
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Yep, that is correct, and a result of me having to rebuild the script from scratch again ...
Remove-Item C:\mp3.csv
foreach ($computer in get-content "C:\computers.txt") {
  if ( !(test-Connection -ComputerName $computer -Count 1 -Quiet) ) {
    Write-Warning "$Server seems dead not pinging"
    "$Computer,*Dead?*,,," | Out-File C:\mp3.csv -Encoding ASCII -Append
  } else { 
    write-Host "$computer is alive and Pinging " -ForegroundColor Green
    [bool] $mp3Found = $false
    $colFiles = Get-Wmiobject -computername $Computer -Query "Select * from CIM_DataFile Where Extension = 'mp3'" -ErrorAction SilentlyContinue |
      ? { $_.FileName } |
      % {
        $filepath = $_.Drive + $_.Path + $_.FileName + '.' + $_.Extension
        $query    = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='$filepath'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
        $objOwner = Get-Wmiobject -computername $Computer -Query $query -ErrorAction SilentlyContinue | Select -First 1
        $user     = $objOwner.ReferencedDomainName + '\' + $objOwner.AccountName
        "$Computer,$filepath,$user,$($objFile.FileSize/1MB),$($objFile.LastModified)" | Out-File C:\mp3.csv -Encoding ASCII -Append
         $mp3Found = $true
      }
    if (!$mp3Found) { "$Computer,*No files*,,," | Out-File C:\mp3.csv -Encoding ASCII -Append }
  }
}

Open in new window

0

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
cwstad2Author Commented:
Thanks guys appreciate the help
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.