我编写的函数mirroring
将调用许多其他函数之一。为了保存重写功能' A',我希望将要调用的函数作为函数' A'进行调用。例如:
function mirroring (
[string] $svr="xxxx",
[string] $inst="MSSQLSERVER2",
[string] $datastore,
[string] $datastore1,
[string] $datastore2,
[string] $datastore3,
[string] $datastore4,
[string] $datastore5,
[string] $datastore6,
[string] $datastore7,
[string] $datastore8,
[string] $datastore9,
[string] $ServerStatus1
)
{
Set-StrictMode -Version 2
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection "Server=$svr\$inst;Database=master;Integrated Security=True;"
$SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = " SELECT db_name(sd.[database_id])AS [Database Name]
,sd.mirroring_state AS [Mirror State Number]
,sd.mirroring_state_desc AS [Mirror State]
,sd.mirroring_partner_name AS [Partner Name]
,sd.mirroring_role_desc AS [Mirror Role]
,sd.mirroring_safety_level_desc AS [Safety Level]
,sd.mirroring_witness_name AS [Witness]
,sd.mirroring_connection_timeout AS [Timeout(sec)]
FROM sys.database_mirroring AS sd
WHERE mirroring_guid IS NOT null
ORDER BY [Database Name];"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0]
$datastore = $DataSet.Tables[0].Rows[0][0]
$datastore1 = $DataSet.Tables[0].Rows[0][1]
$datastore2 = $DataSet.Tables[0].Rows[0][2]
$datastore3 = $DataSet.Tables[0].Rows[0][3]
$datastore4 = $DataSet.Tables[0].Rows[0][4]
$datastore5 = $DataSet.Tables[0].Rows[0][5]
$datastore6 = $DataSet.Tables[0].Rows[0][6]
$datastore7 = $DataSet.Tables[0].Rows[0][7]
if ($datastore2 -eq "Disconnected")
{
DisconnectedREMEDIATION
}
elseif ($datastore2 -eq "SYNCHRONIZED")
{
#$ServerStatus="The Instance Is in Synchronized State"
#$RemActonToBeTaken=1
SYNCHRONIZEDREMEDIATION
}
$ServerStatus1 = "DataBase Name:"+ $datastore+",Mirror State Number"+$datastore1+",Mirror State"+$datastore2+",Partner Name"+$datastore3+",Mirror Role"+$datastore4+",Safety Level"+$datastore5+",Witness"+$datastore6+",Timeout(In Sec)"+$datastore7
InsertServerStatus $ServerStatus1
return $ServerStatus1
$SqlConnection.Close()
}
我的第二个功能是
function InsertServerStatus(
$ServerName="LAPTOP6\MSSQLSERVER2",
$InstanceName,
$ServerStatus1,
$RemActonToBeTaken
)
{
Write-Host $ServerStatus1
}
我的问题是如何在我的函数$ServerStatus1
中使用InsertServerStatus
。我做错了什么?
答案 0 :(得分:0)
Aft是对的,您必须使用所有参数调用该函数。 你应该像这样调用你的函数:
InsertServerStatus $ServerName $InstanceName $ServerStatus1 $RemActonToBeTaken
你在做什么是这样的:
InsertServerStatus $ServerStatus1
所以$ ServerName参数获取ServerStatus1变量的值
答案 1 :(得分:0)
亲爱的所有感谢回复我得到了答案就是那样
将变量名称传递给父函数
$script:ServerStatus1 = ""
进入子函数
function InsertServerStatus(
$ServerName="LAPTOP6\MSSQLSERVER2",
$InstanceName,
$RemActonToBeTaken
)
{
(Parent Function) mirroring
$Script:ServerStatus= $ServerStatus1
"You Code"
}