Using Powershell to loop directories and files

Every once in a while, typically when messing around with VSTS and deployments, I find myself needing to figure out what exactly is up on the server file system.  With this little script, I can get a full load out of all the files and directories from any given path.

$start = (Get-Item -Path ".\" -Verbose).FullName;

function drillDirectory($path) {
    Write-Host "--------------------------------------------";
    Write-Host "Current Directory: $path";
    Write-Host "--------------------------------------------";
    cd $path;
    ls;
    
    Get-ChildItem $path | 
    Foreach-Object {
        if ($_.Mode.StartsWith("d")) {
            drillDirectory($_.FullName);
        }
    }
}

drillDirectory($start);

By using a VSTS Powershell task, I can insert this into my build or release definition and using any variable (e.g. $(System.DefaultWorkingDirectory)) I want to see what’s going on up there. I change the Advanced > Working folder to where I want the script to start and let it do it’s magic.

Example output:

2017-04-25T17:43:57.4736336Z ##[section]Starting: PowerShell Script
2017-04-25T17:43:57.4736336Z ==============================================================================
2017-04-25T17:43:57.4736336Z Task : PowerShell
2017-04-25T17:43:57.4736336Z Description : Run a PowerShell script
2017-04-25T17:43:57.4736336Z Version : 1.2.3
2017-04-25T17:43:57.4746299Z Author : Microsoft Corporation
2017-04-25T17:43:57.4746299Z Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2017-04-25T17:43:57.4746299Z ==============================================================================
2017-04-25T17:43:57.5236291Z ##[command]. 'd:\a\_temp\7c717472-5443-4b1c-b385-5d4ba1038594.ps1' 
2017-04-25T17:43:58.4633932Z --------------------------------------------
2017-04-25T17:43:58.4633932Z Current Directory: D:\a\r1\a
2017-04-25T17:43:58.4633932Z --------------------------------------------
2017-04-25T17:43:58.4935884Z 
2017-04-25T17:43:58.5056030Z 
2017-04-25T17:43:58.5056030Z Directory: D:\a\r1\a
2017-04-25T17:43:58.5066022Z 
2017-04-25T17:43:58.5066022Z 
2017-04-25T17:43:58.5066022Z Mode LastWriteTime Length Name 
2017-04-25T17:43:58.5066022Z ---- ------------- ------ ---- 
2017-04-25T17:43:58.5126028Z d----- 4/25/2017 5:43 PM Support Web Application 
2017-04-25T17:43:58.5196123Z --------------------------------------------
2017-04-25T17:43:58.5196123Z Current Directory: D:\a\r1\a\Support Web Application
2017-04-25T17:43:58.5206144Z --------------------------------------------
2017-04-25T17:43:58.5236214Z 
2017-04-25T17:43:58.5236214Z 
2017-04-25T17:43:58.5236214Z Directory: D:\a\r1\a\Support Web Application
2017-04-25T17:43:58.5236214Z 
2017-04-25T17:43:58.5236214Z 
2017-04-25T17:43:58.5236214Z Mode LastWriteTime Length Name 
2017-04-25T17:43:58.5236214Z ---- ------------- ------ ---- 
2017-04-25T17:43:58.5246023Z d----- 4/25/2017 5:43 PM drop 
2017-04-25T17:43:58.5256206Z d----- 4/25/2017 5:43 PM Extracted 
ETC
ETC
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: