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
thanks
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
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
exactly what I was looking for. thanks. I modified it for my needs but this is what I was looking for. Thanks again!
ASKER
one question how can I use the -Credential parameter with this
like on other functions I have its like 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"
}
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 }}
}
ASKER
I keep getting this error
when using this
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"
}
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:
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.
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.
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"
}
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"
}
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.
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx