POWERSHELL获取目录不包含文件

时间:2014-06-11 05:36:15

标签: powershell

我使用下面的代码找到我的powershell可执行文件所在的位置

$FullPathName=$MyInvocation.MyCommand.Path
function global:GET-SPLITFILENAME ($FullPathName) 
{
$PIECES=$FullPathName.split(“\”) 
$NUMBEROFPIECES=$PIECES.Count 
$FILENAME=$PIECES[$NumberOfPieces-1] 
$DIRECTORYPATH=$FullPathName.Trim($FILENAME) 
return $DIRECTORYPATH
}

我把它作为目录E:\ product \ powershell \

现在在这个目录中我有以下文本文件

E:\product\powershell\Servers.txt
E:\product\powershell\SchedulerTasks.txt

请建议一种简单的方法来查找这些文件并从中获取数据? 我们可以用单行代码消除上述功能吗?

$ServerList = get-content Servers.txt
$SchedulerJobs = get-content SchedulerTasks.txt

另请帮我找到E:\ product \ powershell \

中的所有子文件夹
we have
 E:\product\powershell\ps1
 E:\product\powershell\ps1\code
 E:\product\powershell\ps1\db
 E:\product\powershell\web\code
 E:\product\powershell\web\code\a
 E:\product\powershell\web\code\a\b
 E:\product\powershell\web\code\a\b\c
 etc

I was using something like this 
 $table = get-childitem "powershell" -recurse | where { $_.PSIsContainer}

The location is E:\product\powershell\ is not fixed it may vary based on location of   
powershell executable

请帮忙

2 个答案:

答案 0 :(得分:0)

您真的很努力地获得构建到PowerShell中的结果:

Split-Path $MyInvocation.MyCommand.Path

IMO如果您不想在脚本目录中的文件中提供脚本中的完整路径,可以使用Push/Pop-Location

Split-Path $MyInvocation.MyCommand.Path | Push-Location
$ServerList = Get-Content Servers.txt
$SchedulerJobs = Get-Content SchedulerTasks.txt

# ... do your stuff...

Pop-Location

答案 1 :(得分:0)

试试这个

$ServerList=join-path (split-path $myinvocation.mycommand.path -parent) servers.txt