0

I try to copy newest file from network folder and execute it. In location where I need to copy 2017 file I have 2015 file, but I need to execute only 2017 file.

# Copyfile from network shared folder to folder in host machine

Get-ChildItem "\\r\Pro\Al\Ort\Daily B\2017\x64" -Filter *.exe | Where Name -NotMatch '.*NoDB\.exe$' | % {
New-Object psobject -Property @{
    No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
    Name = $_.FullName
}

 } | Sort No -Descending | Select -ExpandProperty Name -First 1 | Copy-Item -Destination "C:\Users\User Name\Desktop" 


# Execute .exe file

Get-ChildItem "C:\Users\User Name\Desktop" -Include *exe*, *2017* | Where Name -NotMatch '.*NoDB\.exe$' | % {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}

} | Sort No -Descending | Select -ExpandProperty Name -First 1 | 
Foreach { & $_ -s2 -sp"-SilentInstallation=server -UpdateMaterials=yestoall -UpgradeDBIfRequired=yes"}

When I execute this script nothing happens.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
Mikhail R
  • 391
  • 2
  • 6
  • 17
  • Likely one of your cmdlets is not returning anything so you later cmdlets in the pipeline are not executing. Can you confirm the file exists? Also, `-include` and `-exclude` work better with `-recurse`. I think a filter would be better here. `Get-ChildItem "C:\Users\User Name\Desktop" -Filter "*2017*.exe"`. Can you confirm the files exist where you expect them to be? I think we need some more debugging from you to help. The code otherwise _appears_ OK. – Matt Jul 05 '16 at 15:37
  • Matt, yes I see file and it exists. After I changed `-include` to `-filter` and used `"*2017*.exe"` instead of `"*exe*, *2017*"` - it works perfectly! If you don't mind, make an answer from your reply and I'll vote for it as an answer! – Mikhail R Jul 05 '16 at 15:45
  • No need. If that is your issue then the dupe covers the issue. Glad it is working for you. – Matt Jul 05 '16 at 15:49
  • Thanks again, Matt. And for future - if I want to count some text values and numbers should I use such syntax `"*2017*, *CL*, .exe"`? – Mikhail R Jul 05 '16 at 15:52
  • If your filter starts getting complicated you can post process with `Where-Object` like you are with your examples above. That filter has no wildcards so you would only return the file named that exactly. – Matt Jul 05 '16 at 15:54
  • Matt, thanks again! – Mikhail R Jul 05 '16 at 15:55

0 Answers0