0

Need to be able to run .sql files individually that are located in multiple sub directories. This script will be ran on multiple computers where the directory wont be the same but the sub directories will be. So I want to be able to define the directory as a variable like this.

$Path = 'D:\Source\Database'
invoke-sqlcmd -Username $username -Password $password -inputfile "$Path*\SQLServer\create_types.sql" -serverinstance "localhost" -database "test" | Out-File -FilePath "c:\testoutput.txt"

1 Answer 1

2

Use Get-ChildItem with -recurse and pipe into a ForEach loop.

$Path = 'D:\Source\Database'
Get-ChildItem "$Path\*\SQLServer\create_types.sql" -recurse|ForEach{
    invoke-sqlcmd -Username $username -Password $password -inputfile $_.FullName -serverinstance "localhost" -database "test"
} | Out-File -FilePath "c:\testoutput.txt"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.