[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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
0
bbimis
Asked:
bbimis
  • 3
  • 2
1 Solution
 
JAN PAKULACommented:
save your script and run it on the remote computer using psexec


http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
0
 
footechCommented:
You can either access the remote machine via UNC path, or you can use PowerShell Remoting.
You wouldn't use ? {$_.PSIsContainer} as that would only give you folders.
If you have a lot of machines, PS Remoting is by far the better solution in terms of speed, but it takes some setup.  If you just have one or a few machines, accessing via the UNC only requires admin permissions and ports opened for file sharing.
Get-ChildItem \\remotecomp\c$ -filter *.jpg -recurse | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)}

Open in new window

If you needed to specify an upper-limit to the date range that is less than the current time, then you would just need to add another condition to the Where-Object scriptblock.
0
 
bbimisAuthor Commented:
exactly what I was looking for. thanks. I modified it for my needs but this is what I was looking for. Thanks again!
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
bbimisAuthor Commented:
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

0
 
bbimisAuthor Commented:
I keep getting this error
error i get
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

0
 
footechCommented:
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.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now