Peter Koller
Peter Koller

Reputation: 310

Powershell script not executing with parameters

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?

Upvotes: 0

Views: 355

Answers (1)

Victor Silva
Victor Silva

Reputation: 780

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.

Upvotes: 4

Related Questions