简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM