Hello! I am reaching out as I am at a loss. I am trying to create a Powershell script that gets a upc code from the user. The upc code is then used in a SQL query to grab data. The script below does not produce any errors yet does not produce any results. All data in the view is blank after I run the code. I’m pretty sure it has something to do with the passing of the variable yet I am lost.
Write-Host "Start of script"
$getUPC = Read-Host -Prompt 'Please enter the full UPC number including zeroes.'
$upcNum = $getUPC
#Check variable upcNum
Write-Output $upcNum
function processView
{
Invoke-Sqlcmd -Query {ALTER VIEW kitFinder AS
(select t1.PLU_NUM, t1.PLU_DESC, t2.MasterUPC as "Kit MasterUPC", t2.ItemDescription as "UPC Description in Kit" from PLU t1 inner join kit t2 on t1.PLU_NUM = t2.ItemUPC Where PLU_NUM = '$upcNum')} -ServerInstance xxx.xxx.xxx.xxx -Username "admin" -Password "admin" -Database corp_DB
}
processView
Please observe the instructions for formatting code; [PRE] isn’t it. Instructions appear immediately above the posting textbox.
I’m not personally an enormous fan of Invoke-SqlCommand for running queries; you might instead look at https://leanpub.com/ditchexcelmakinghistoricalandtrendreportsinpowershell, which is an ebook I wrote (its free) that covers data manipulation using the underlying .NET Framework classes. The book (and its code module) have plenty of examples you can likely adapt.
I find a key, for me, is to build my SQL in a variable. That way, I can both execute it but also output just the final string (using Write-Verbose, for example), allowing me to see exactly the query being sent to SQL, after all my variables are inserted. It makes debugging complex queries a ton easier, as I can quickly copy and paste the actual query into SQL Management Studio and execute it there to test it.
The only disadvantage you have using Invoke-SQLCmd is that you need to ensure that the SQLPS module is available when\where you execute the script. For simple SQL commands, it works well. If you require that it runs on with no software\module pre-req, then you can investigate using .NET or other database options.
With that said, this is how I format and use my SQL commands that I use in prod in multiple companies:
function Invoke-KitFinderUpdate {
param (
[string]$UPC
)
begin {
Import-Module -Name SQLPS
}
process {
#You cannot indent here-strings :(
$sqlCmd = @"
ALTER VIEW
kitFinder
AS
SELECT t1.PLU_NUM
,t1.PLU_DESC
,t2.MasterUPC AS 'Kit MasterUPC'
,t2.ItemDescription AS 'UPC Description in Kit'
FROM PLU t1 inner join kit t2 on t1.PLU_NUM = t2.ItemUPC
Where PLU_NUM = '$UPC';
"@
$sqlParams =@{
Query = $sqlCmd
ServerInstance = 'xxx.xxx.xxx.xxx'
Username = 'admin'
Password = 'admin'
Database = 'corp_DB'
}
Invoke-Sqlcmd @sqlParams
}
end {}
}
$getUPC = Read-Host -Prompt 'Please enter the full UPC number including zeroes.'
Invoke-KitFinderUpdate -UPC $getUPC
Thank you all for the help! It looks like it’s working now. I created the view as I have to get data from three tables but do a second pass on one of them.