Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

search for file type in a date range using powershell remotely

how can I search for say *.jpg on a remote system from say 8-12 to 8-18 ?
I was thinking something along these lines but not sure how to pass credentials and all
dir "c:\" | ? {$_.PSIsContainer} | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | ConvertTo-Html Name | Set-Content c:\tony\new_Media.htm

Open in new window


thanks
Avatar of JAN PAKULA
JAN PAKULA
Flag of United Kingdom of Great Britain and Northern Ireland image

save your script and run it on the remote computer using psexec


http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis
bbimis

ASKER

exactly what I was looking for. thanks. I modified it for my needs but this is what I was looking for. Thanks again!
Avatar of bbimis

ASKER

one question how can I use the -Credential parameter with this
function find_file {
param ( $RemoteComputer = $compname )
$searchfor = Read-Host "What would you like to search for? Wild cards excepted"
  $output = Get-ChildItem \\$RemoteComputer\c$ -filter  $searchfor -recurse | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Format-List
  $output | Out-File "filesearch results for $RemoteComputer.txt"
  }

Open in new window


like on other functions I have its like this
function Get-IPConfig{

param ( $RemoteComputer= $compname,

  $OnlyConnectedNetworkAdapters=$true

   )

gwmi -Class Win32_NetworkAdapterConfiguration -ComputerName $RemoteComputer -Credential $creds | Where { $_.IPEnabled -eq $OnlyConnectedNetworkAdapters } | Format-List @{ Label="Computer Name"; Expression= { $_.__SERVER }}, IPEnabled, Description, MACAddress, IPAddress, IPSubnet, DefaultIPGateway, DHCPEnabled, DHCPServer, DNSServerSearchOrder, @{ Label="DHCP Lease Expires"; Expression= { [dateTime]$_.DHCPLeaseExpires }}, @{ Label="DHCP Lease Obtained"; Expression= { [dateTime]$_.DHCPLeaseObtained }}

} 

Open in new window

Avatar of bbimis

ASKER

I keep getting this error
User generated image
when using this
function find_file {
    param ( $RemoteComputer = $compname)
    $credits = Get-Credential
    $searchfor = Read-Host "What would you like to search for? Wild cards excepted"
    $output = Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    Get-ChildItem \\$RemoteComputer\c$ -filter  $searchfor -recurse | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Format-List
                    } -Credential $credits
     $output | Out-File "filesearch results for $RemoteComputer.txt"
       
  }

Open in new window

Since Get-ChildItem doesn't support the -credential parameter, the only way you could use the code in http:#a40270200 would be to launch the PS console using different credentials.

In http:#a40270254, that is using PS Remoting.  It is an option but needs to be enabled.  Typically you would use something like:
function find_file {
    param ( $RemoteComputer = $compname)
    $credits = Get-Credential
    $searchfor = Read-Host "What would you like to search for? Wild cards excepted"
    $output = Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    Get-ChildItem c:\ -filter  $searchfor -recurse | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Format-List
                    } -Credential $credits
     $output | Out-File "filesearch results for $RemoteComputer.txt"
       
  }

Open in new window

You wouldn't have the session be on the remote computer and use that computer's UNC path at the same time.

One other option is to use Invoke-Command to create a session on the local machine using different credentials, and then query the UNC path.  This would have to be run from an elevated session in order to use Invoke-Command to the local machine.
function find_file {
    param ( $RemoteComputer = $compname)
    $credits = Get-Credential
    $searchfor = Read-Host "What would you like to search for? Wild cards excepted"
    $output = Invoke-Command -ComputerName localhost -ScriptBlock {
                    Get-ChildItem \\$RemoteComputer\c$ -filter  $searchfor -recurse | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} | Format-List
                    } -Credential $credits
     $output | Out-File "filesearch results for $RemoteComputer.txt"
       
  }

Open in new window


Last note - if you're just going to output to a file, there's no reason to first store the result in a variable like $output, just pipe straight to Out-File.