0

When I execute attempt to execute the script provided below, I get nothing back, no errors, nothing. And no changes are executed. I could use some help with this. Here is the script:

Function Insert-DefaultAddressesToKretaDb{
Param(
[parameter(Mandatory=$true)]
[string]$server,
[parameter(Mandatory=$true)]
[string]$database) 

Write-Host "Executing on "  $server " database " $database
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$server';database='$database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Sql = "<validated and working sql script>" 
$Command.CommandText = $Sql
$Command.ExecuteNonQuery()
$Connection.Close()
}

And this is how i tried to execute:

. .\Insert-DefaultAddressesToKkopDb.ps1 -server "<serverName>'-database "<databaseName>"

Any thoughts?

1 Answer 1

4

I think that you confuse 2 concepts: function and script. In the first code block, you build a function. For call it, you can run this:

Function Insert-DefaultAddressesToKretaDb{
Param(
[parameter(Mandatory=$true)]
[string]$server,
[parameter(Mandatory=$true)]
[string]$database) 

Write-Host "Executing on "  $server " database " $database
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$server';database='$database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Sql = "<validated and working sql script>" 
$Command.CommandText = $Sql
$Command.ExecuteNonQuery()
$Connection.Close()
}
#Call the function
Insert-DefaultAddressesToKretaDb -server "<serverName>'-database "<databaseName>"

You might call your function.

Exist another way, this is create a script. For this, you save the following with the name Insert-DefaultAddressesToKretaDb.ps1:

Param(
[parameter(Mandatory=$true)]
[string]$server,
[parameter(Mandatory=$true)]
[string]$database) 

Write-Host "Executing on "  $server " database " $database
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$server';database='$database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Sql = "<validated and working sql script>" 
$Command.CommandText = $Sql
$Command.ExecuteNonQuery()
$Connection.Close()

And, after that, you can run this:

.\Insert-DefaultAddressesToKretaDb.ps1 -server "<serverName>'-database "<databaseName>"

I guess the correct name of the function/script is Insert-DefaultAddressesToKretaDb.

Sign up to request clarification or add additional context in comments.

1 Comment

That answer has successfully clarified my misunderstanding. It is working now. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.