简体   繁体   中英

Trying to get latest folder name in Powershell

I am attempting to run nuint console from powershell to run some Selenium Tests on my app.

The commmand in Powershell is as below:

$command = 'D:\tools\NUnitTestRunner\nunit3-console.exe "\\myserver\Drops\MyProj\MyApp_20170214.1\App.Selenium.Tests.dll"'
iex $command

This works as expected. However, the piece I want to change is in the double "". I want to run my Regression Test when a release is dropped to the folder a new App.Selenium.Test.dll will get dropped - with the Folder MyApp_DATE.DropNumber will change.

So I may have under the path \\myserver\\Drops\\MyProj\\ folders like below:

MyApp_20170214.1
MyApp_20170214.2
MyApp_20170214.3
MyApp_20170214.4

I want to get the latest folder dynamically and put it into the command rather than having to go in each time and hard code it. This is what I have tried:

$logFile = "$PSScriptRoot\NunitLog.txt"
$dir = "\\myserver\Drops\MyProj\"

#Go get the latest Folder Name
$latest = Get-ChildItem $dir Where { $_.PSIsContainer } | Sort CreationTime -Descending | Select -First 1

#remove old log file
if(Test-Path $logFile) { Remove-Item $logFile }

"Starting Selenium Tests" | Out-File $logFile -Append
$latest.name | Out-File $logFile -Append

#Start nUnit Console and pass argument which is the latest path to the dll of the tests
#$command = 'D:\tools\NUnitTestRunner\nunit3-console.exe "$latest.name"'

However, it isn't outputting the folder name to the logFile or running it in the command

Your code was missing a pipe between dir & Where, when populating $latest. Also, the $command line should have double quotes round the entire string, as PowerShell treats it as a string literal when wrapped in single quotes. To include double quotes in the resulting command, we add a backtick before them. We also wrap $latest.name with $() to allow PowerShell to evaluate - otherwise we end up with the folder name having .name at the end of it.

$logFile = "$PSScriptRoot\NunitLog.txt"
$dir = "\\myserver\Drops\MyProj\"

#Go get the latest Folder Name
$latest = Get-ChildItem $dir | Where { $_.PSIsContainer } | Sort CreationTime -Descending | Select -First 1

#remove old log file
if(Test-Path $logFile) { Remove-Item $logFile }

"Starting Selenium Tests" | Out-File $logFile -Append
$latest.name | Out-File $logFile -Append

#Start nUnit Console and pass argument which is the latest path to the dll of the tests
#$command = "D:\tools\NUnitTestRunner\nunit3-console.exe `"$($latest.name)`""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM