Frage

Background

There is a directory that is automatically populated with MSI files throughout the day. I plan on leveraging Task Scheduler to run the script shown below every 15 minutes. The script will search the directory and copy any new MSIs that have been created in the last 15 minutes to a network share.

Within this folder C:\ProgramData\flx\Output\<APP-NAME>\_<TIME_STAMP>\<APP-NAME>\ there are two other folders: Repackaged and MSI Package. The Repackaged folder does not need to be searched as it does not contain any MSIs. Also I have found that it needs to be excluded in some way to prevent this error:

Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:14 char:32
+$listofFiles=(Get-ChildItem <<<< -Recurse -Path $outputPath -Include "*.msi" -Exclude "*.Context.msi" | where {$_.LastAccessTime -gt $time.AddMinutes($minutes)})
+ CategoryInfo : ReadError: C:\ProgramData\...xcellence\Leg 1:String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

Limitations

  • I am stuck using Powershell v1.0
  • I have no control over the directory structure of the source location

Updated:

  • I don't know the app name or the what the time stamp will be. That is something else that is out of my control.

Current plans

I have read about using -Filter and I am aware of filters that are similar to functions but I wasn't able to come up with any ideas of how to use them. My only thought at the moment would be to do something like:

$searchList=Get-ChildItem "all instances of the MSI Package folder"

foreach($folder in $searchList){
    $listofFiles=Get-ChildItem "search for *.msi"

    foreach($file in $listofFiles){"Logic to copy MSI from source to destination"}
}

However...I thought that there might be a more efficient way of doing this.

Questions

  1. How can I limit depth that Get-ChildItem searches?
  2. How can I limit the Get-ChildItem search to C:\ProgramData\flx\Output\<APP-NAME>_<TIME_STAMP>\<APP-NAME>\MSI Package
  3. How can I only search folders that have been accessed in the last 15 minutes? I don't want to waste time drilling down into folders when I know MSI has already been copied.

Any additional advice on how to make this script more efficient overall would also be greatly appreciated.

Script

My current script can be found here. I kept getting: "Your post appears to contain code that is not properly formatted as code" and gave up after the fourth time trying to reformat it.

War es hilfreich?

Lösung 2

With help from C.B. this is my new search which eliminates the issues I was having.

  • Changed -Path to C:\ProgramData\flx\Output\*\*\*\* to help limit the depth that was searched.
  • Used -Filter instead of -Include and put the -Exclude logic into the where clause.

Get-ChildItem -Path C:\ProgramData\flx\Output\*\*\*\* -Filter "*.msi" | where {$_.Name -notlike "*.Context.msi" -and $_.LastAccessTime -gt (Get-Date).AddMinutes(-15)}

Andere Tipps

You can try this

dir C:\ProgramData\flx\Output\*\*\*\*\* -filter *.msi 

this search all .msi files at this level

C:\ProgramData\flx\Output\<APP-NAME>\_<TIME_STAMP>\<APP-NAME>\Repackaged or 'MSI Package' or whatever else present folder

without recursion, this avoid too deep folder that give you error.

Pipe the result to:

Where {$_.LastAccessTime -gt (Get-Date).AddMinutes(-15)} #be sure no action on file is taken before the dir command

or

Where {$_.LastWriteTime -gt (Get-Date).AddMinutes(-15)} #some file can be re-copied maybe

You can't limit the recursion depth of Get-ChildItem except to not use -Recurse i.e. Get-ChildItem is either depth = 0 or N.

Set up variables for app name and timestamp e.g.:

$appName = "foo" 
$timestamp = Get-date -Format HHmmss
Get-ChildItem "C:\ProgramData\flx\Output\${appName}_$timestamp\$appName\MSI Package" -force -r

You can filter the results like so:

Get-ChildItem <path> -R | Where {$_.LastWriteTime -gt (Get-Date).AddMinutes(-15)}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top