"Invoke-Sqlcmd | Out-File" not in matrix format? - sql

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

Related

How to pull SQL row with multiple values comma separated into PowerShell array to be used in Get-ChildItem -exclude

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

DBATools - Remove timestamp from Export-DBAScript -PATH

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?

Invoke-Sqlcmd not working in SQL

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 ^

Powershell invoke-sqlcmd from query in a txt file

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
}

Powershell - using wildcards to search for filename

I am trying to make a PowerShell script that will search a folder for a filename that contains a certain file-mask. All files in the folder will have format like *yyyyMd*.txt.
I have made a script:
[String]$date = $(get-date -format yyyyMd)
$date1 = $date.ToString
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*$date1*'}
But this does not seem to work..
Can anyone help? It seems the problem is that the date variable is not correct because when I hard code something like below, it works:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_.Name -like '*20141013*'}
You can simplify this by just using regex with the -match operator:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where-Object {$_ -match (Get-Date -format yyyyMMdd)}
And if you are on V3 or higher, you can further simplify to:
Get-ChildItem C:\Users\pelam\Desktop\DOM | Where Name -match (Get-Date -format yyyyMMdd)