I have TSQL insert statements in a multiple text file in a folder. I need to run on sql server using foreach loop in powershell. it should go to each file in a folder read the query execute that and terminate the connection again read the next file and do the same operation
invoke-sqlcmd -ServerInstance KUMSUSHI7 -Query (Get-Content | For-each "C:\Drill\Task\SAMS Automation\Query.txt")
Please help me on this
Invoke-Sqlcmd can take script file as a parameter, so no -Query is needed. There's an example on MSDN:
Invoke-Sqlcmd -InputFile "C:\TestSQLCmd.sql" | Out-File -filePath "C:\TestSQLCmd.rpt"
Thus, get a list of your sql files and pass the file names to Invoke-Sqlcmd like so,
gci "c:\some\path\*" -include *.sql | % {
Invoke-Sqlcmd -InputFile $_.FullName
}
Related
PowerShell newbie trying to figure out how to pull SQL row with multiple values comma separated into PowerShell array to be used in Get-ChildItem -exclude.
Please note I have the SQL portion working to where I can pull the values into PowerShell, but no matter what I have tried when I try to pass it to the -exclude option it thinks its just one name, versus comma separated names. I need help understanding how to pass the SQL row values into an array like this so it can be used properly with -exclude
View of hardcoded array that works
https://i.stack.imgur.com/9Fnsh.png
View of SQL table
https://i.stack.imgur.com/bNuKy.png
Code that gets it from SQL
https://i.stack.imgur.com/hCP2P.png
View of PS code to remove folders using the -ExcludeFolder option https://i.stack.imgur.com/dfvlK.png
$ExcludeFolder = $row.ExcludeFolder
Get-ChildItem -Path $Source -Recurse -Force -Exclude $excludeFolder | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null -and $_.LastWriteTime -lt $CutDay} | Remove-Item -Force -Recurse
Write-Host "End Time:" (Get-Date)
}
Thanks for any help you can provide.
SK
If you have a string in your $row.ExcludeFolders variable that reliably includes commas, you can use similar to the following to create an array:
You might then be able to pass the array as the -Exclude value
Is it possible to remove the timestamp when Export-DBAScript -PATH is used? I would just like to use the Server Name and Date.
For example this is what I want to export:
Get-DbaAgentJob -SqlInstance $ServerList| EXPORT-DBASCRIPT -PATH $FILENAME -Append
This is what my Output File is named (I would like to not have the timestamp):
Is this is possible, and if so how I would go by achieving this?
The following two inline yaml-pipeline powershell scripts:
- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "2.0.0.0", '$(major).$(minor).$(patch).0'} | set-content -path $(versionHeader)
displayName: 'Update build number in header file'
- pwsh: (get-content -path $(versionHeader)) | foreach-object {$_ -replace "20200101000000", (get-date -f 'yyyyMMddhhmmss')} | set-content -path $(versionHeader)
displayName: 'Update date in header file'
are meant to take these two lines
[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.0.0")]
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200101000000")]
and turn them into these two lines (i.e. put new values in the quotes)
[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.185.0")]
[assembly: MyApp.Net.Attributes.BuildDateAttribute("20200724013502")]
(The replacement values vary)
And either script works fine by itself. But when I try to use both scripts, one after the other, the second value comes out messed up.
[assembly: MyApp.Net.Attributes.AssemblyVersion("2.0.209.0")] // correct
[assembly: MyApp.Net.Attributes.BuildDateAttribute("202.0.209.000000")] // ?????
Obviously they are somehow interfering with each other but I don't know how. Can someone tell me what I'm doing wrong?
The problem is in powershell's -replace - it interprets wildcards, and it happens that 20200101000000 matches 2.0.0.0:
PS> "20200101000000" -replace "2.0.0.0", "zzzzzz"
20zzzzzz00000
I am using powershell to query a SQL table and then save the results in a file, this is working fine, however, the results are not stored in a matrix format but rather chunks.
How can I save the output like normal rows with separators tabs?
Invoke-Sqlcmd -ServerInstance "localhost" -Database "mydb" -InputFile "C:\Delivery.sql" | Out-File -filePath "C:\Delivery.txt
You should be using the Export-Csv CmdLet to perform this action:
Invoke-Sqlcmd -ServerInstance "localhost" -Database "mydb" -InputFile "C:\Delivery.sql" |
Export-Csv -Path "C:\Delivery.txt
Im using the following command in SQL Powershell to generate csv of data returned by SQL.
Invoke-Sqlcmd -Query "myp" -ServerInstance "." | Export-Csv -Path "d:\data\MyDatabaseSizes.csv" -NoTypeInformation
The above command works perfectly fine in SQL Power Shell. but when I tried to run in from SQL using the following code
DECLARE #cmd varchar(1000)
SET #cmd = 'Invoke-Sqlcmd -Query "myp" -ServerInstance "." | Export-Csv -Path "d:\data\MyDatabaseSizes.csv" -NoTypeInformation'
EXEC xp_cmdshell #cmd
it give error that
'Invoke-Sqlcmd' is not recognized as an internal or external command,
Anyone please help me in running the command from SQL.
Thanks
xp_cmdmshell executes Windows shell commands, not PowerShell commands. You can of course call PowerShell from within the Windows shell, for example like this:
DECLARE #cmd varchar(1000)
SET #cmd = 'Invoke-Sqlcmd -Query ''myp'' -ServerInstance ''.'' ^| Export-Csv -Path ''d:\data\MyDatabaseSizes.csv'' -NoTypeInformation'
EXEC xp_cmdshell 'powershell.exe -c "' + #cmd + '"'
Note that:
I have replaced the double quotes in the PowerShell command with single quotes (escaped for SQL) so I can use double quotes in the Windows command. If you need double quotes in the PowerShell, you're better off having the PowerShell script in a file to avoid all this
You may also need more parameters (e.g. to set the execution policy). This MSDN page has more help on PowerShell command line switches
Pipe characters in command line arguments need to be escaped with ^