Powershell脚本不使用参数执行

时间:2018-03-22 09:56:16

标签: powershell

当我执行尝试执行下面提供的脚本时,我什么都没有回来,没有错误,什么都没有。并且不会执行任何更改。我可以用一些帮助。 这是脚本:

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()
}

这就是我试图执行的方式:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

我认为你混淆了两个概念:功能和脚本。在第一个代码块中,您构建一个函数。要打电话,你可以运行:

b's addr: 0x10414020
addr in a[0]: 0x10414020
b: 2

你可以打电话给你的功能。

另一种方式,这是创建一个脚本。为此,您使用名称​​ Insert-DefaultAddressesToKretaDb.ps1 保存以下内容:

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>"

然后,你可以运行:

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()

我想函数/脚本的正确名称是Insert-DefaultAddressesToKretaDb。