PowerShell Query Help

I have the following code. The full script, amongst other things, imports data from various SQL scripts into a query that it generates and saves as a .SQL script. The variable $ScriptRoot is the 'root' directory that contains the file obectOrder.txt.

The file objectOrder.txt is a list of the individual scripts in the format of path\filename. Path is relative to $ScriptRoot, so if the value of $ScriptRoot is 'C:\Users\username\My Documents\SQL' and objectOrder.txt had a row that read 'tables\mytable.sql' then there should be a script named 'mytable.sql' in the directory 'C:\Users\username\My Documents\SQL\tables\'

The code below makes sure that all scripts listed in objectOrder.txt exist in their respective folders and also makes sure that all scripts in the defined folders are listed in objectOrder.txt (to make sure a developer does not add a script to the project but not to the objectOrder.txt file.

Right now, the PowerShell script executes in the directory defined by the variable $ScriptRoot, but if I move it, then it no longer will work, as it is looking for files off the execution path.

I want to revise the code below so it will still function properly (i.e. all paths/files are relative to $ScriptRoot) regardless of where the PowerShell script is located. It is probably a relatively simple change but I've played around with it quite a bit and all I've succeeded in doing is generating a lot of errors.

	$objectOrder = "$ScriptRoot\objectOrder.txt"
	$fileList = Get-Content -Path $objectOrder
	$directoryList = $fileList | %{ $_.split("\")[0]} | select -unique
	$files = $directoryList | %{ Get-ChildItem $_ } | ?{ $_.extension -eq ".sql" } | %{"$(\$_.Directory.Name)\$($_.Name)" }

	# compares files in tables directory to provided list, shows differences and offers the option of exiting the script if
	# there are. the option to continue should only be chosen if all files in the list are present in the tables folder
	$fileDiff = Compare-Object $fileList $files
	if ($fileDiff) {
		Write-Host "`n"
		Write-Host "Please Verify the following files have both a list entry and a file: `n`n"
		$fileDiff | foreach {
			if ($_.SideIndicator -eq "=>") {
				$_.InputObject + "  (file not listed in ojbectOrder.txt)"
			} 
			else {
				$_.InputObject + "  (listed file not located in indicated directory)"
			}
		}

		if (!$NoHead)
			{
			throw "Incongruity between objectOrder.txt and directory structure/files"
			}
		else
			{
			$title="objectOrder.txt Missing!"
			$message = "Continuing with missing files will likely produce errors.`nChoose 'Yes' only if all files in the list are present.`n`nNote: It is NOT recommended that you continue`nwithout resolving the missing files!"
			$continue = [system.windows.forms.messagebox]::show($message,$title, "YesNo", "Warning", "Button2", "ServiceNotification")
			switch ($continue)
				{
				"Yes" {}
				"No" {throw "Exiting due to user response! Missing table file(s) detected."}
				}
			}
		}

Open in new window

LVL 15
dbbishopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Unless the current directory is $scriptroot, then on line 7 "Get-ChildItem $_" won't work.  Also, there is an extra backslash in there which shouldn't be (corrected below).
%{"$($_.Directory.Name)\$($_.Name)" }

Open in new window

There are two choices to correct the "Get-ChildItem $_" bit; either
- change location to $scriptroot before running that command, or
- submit the full path to Get-ChildItem, i.e.
$files = $directoryList | %{ Get-ChildItem (Join-Path $ScriptRoot $_) } | ?{ $_.extension -eq ".sql" } | %{"$($_.Directory.Name)\$($_.Name)" }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbbishopAuthor Commented:
The extra backslash was a remnant of me trying to figure out how to get the full path and not the original code. Thanks for pointing it out.

I like the idea (why didn't I think of it) of just chaging the location. DUH!! Although asking this question exposed me to Join-Path. How in the world does one remember all this stuff. I'm doing good to remember how to load PowerShell :-)
0
dbbishopAuthor Commented:
Thanks. I just used set-location after I processed the parameter file (didn't mention that part, did I). Works like a charm. I think I tried to over-complicate the task at hand :-)
0
footechCommented:
It's about as easy to use the -join operator as it is to use Join-Path (and -split instead of Split-Path), and there's been times where I've forgotten about certain cmdlets, or nuances of different operators.  Being exposed to questions from others that I wouldn't have to deal with in my day-to-day work helps me to remember a lot of stuff that I would otherwise probably forget.  I also find that I revisit certain help topics repeatedly.  Even browsing a list of cmdlets can help.  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.