![]() |
#1 |
Участник
|
waldo: Invoke-NAVSQL: Execute SQL queries on NAV databases with PowerShell
Источник: http://dynamicsuser.net/nav/b/waldo/...ith-powershell
============== Just another CmdLet to bother you with (no really, it’s quite an interesting one – try to keep awake … ;-)). The more you use PowerShell .. and the more you use it for NAV .. you just realize that you have to call out to SQL Server quite regularly. Just a few examples:
Goals Simplicity is key here – I just want to provide a ServerInstance, and the function needs to figure out how to connect to the database. Next, if I had some kind of “select” statement, it needed to be simple to get to the data. As you know, PowerShell returns Objects .. so in case of this new function, columns needed to be properties, and records needed to be elements in my resulting object collection. Invoke-NAVSQL I decided to call the function “Invoke-NAVSQL” – according to a similar existing function part of the SQLPS module. You can find the function on my github. A few major things that stand out in the script:
Some examples To have a look at the UidOffSet property of our database that is attached to server instance “DynamicsNAV90”: $dbproperties = Invoke-NAVSql -ServerInstance 'dynamicsNAV90' -SQLCommand 'Select * From [$ndo$dbproperty]'$dbproperties.uidoffsetGet all companies, and get the customers of the first company: $Mycompanies = Invoke-NAVSql -ServerInstance 'dynamicsNAV90' -SQLCommand 'Select * From Company'$Customers = $Mycompanies | select -First 1 | foreach{ Invoke-NAVSQL -ServerInstance 'dynamicsNAV90' -SQLCommand "select * from [$($_.Name)$('$Customer')]" -ShowWriteHost }$Customers | select 'No_', Name, 'Credit Limit (LCY)' | Format-Table -AutoSizeAssumptions I know .. “Assumption is the mother of all fuckups“. But simplicity often comes with a number of assumptions, doesn’t it? ;-). My big assumption is the security-part. Namely, I assume the PowerShell user (Windows Authentication) has got access to the SQL Server, and obviously enough permissions to perform the query. Источник: http://dynamicsuser.net/nav/b/waldo/...ith-powershell
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|