【发布时间】:2020-02-19 15:22:28
【问题描述】:
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=TSG-ADLT014;Integrated Security=true;Initial Catalog =GenExAll"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "exec usp_GetAccountForRemoteMailbox"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
$name = "test"
$LoginId = "jsmith"
$Userid = "14"
write-host "Enable-RemoteMailbox ""$name"" -RemoteRoutingAddress ""$LoginId@wkhs.mail.onmicrosoft.com"""
现在我只是传递参数中的值,$name、$loginid、$userID。我需要将 sp 的输出存储到一个变量中,然后使用该变量。
This image is the output of the sp, sp is outputting Userid, Loginid, Name
运行代码:
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=TSG-ADLT014;Integrated Security=true;Initial Catalog =GenExAll"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "CREATE TABLE [dbo].[Ident_TESTTABLE](
[UserId] [int] NOT NULL,
[LoginId] [nvarchar](128) NOT NULL,
[Name] [nvarchar](2000) NULL
) INSERT INTO Ident_TESTTABLE VALUES(12,'ATEST','AMSAL TEST') SELECT top 1 * FROM [Ident_TESTTABLE] "
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
$name = "test"
$LoginId = "jsmith"
$Userid = "14"
write-host "Enable-RemoteMailbox ""$name"" -RemoteRoutingAddress ""$LoginId@wkhs.mail.onmicrosoft.com"""
write-host $name
write-host $LoginId
write-host $Userid
【问题讨论】:
标签: sql powershell