D B
asked on
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.
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."}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :-)
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. :)
ASKER
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 :-)