【问题标题】:Saving SP output into a variable in powershell将 SP 输出保存到 powershell 中的变量中
【发布时间】: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


    【解决方案1】:

    这...

    write-host "Enable-RemoteMailbox ""$name"" -RemoteRoutingAddress ""$LoginId@wkhs.mail.onmicrosoft.com"""
    

    ...除了写到屏幕上什么都不会做。

    这就是你想要做的……

    $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]| 
    ForEach {Enable-RemoteMailbox $PSItem.name -RemoteRoutingAddress $PSitem.LoginId@wkhs.mail.onmicrosoft.com}
    

    意思是,从 SP 获取输出,因为它恰好启用了 O35 邮箱? 没有理由为了使用它而输出它。好吧,除非你愿意,甚至应该让你做 $PSItem.name 等。没有真正的理由额外变量,恕我直言。

    哦,你的意思是这样的......

    $SqlQuery = "select Server from ALLdevices where SerialNumber = '$($item.SerialNumber)'"
    

    或者这个……

    $SerialNumber='234978'
    $SqlQuery=@"
    SELECT Server
    FROM ALLdevices
    WHERE SerialNumber = '$SerialNumber'
    "@
    $SqlQuery
    

    这样的例子遍布网络。以上只是我保存在 OneNote 文件中的内容。搜索说这个...

    'pass PowerShell variable to a sql query'

    或者这个……

    'pass PowerShell variable to a SQL stored procedure'

    ...会给出这样的例子...:

    Using Powershell Variables in a File-Stored Query with SQL Server

    SQL Server - Trouble passing variable to a stored procedure

    Pass a powershell variable into a SQL value during out-datatable (invoke-sqlcmd2)

    【讨论】:

    • 我要做的是存储登录 ID 并将其传递给存储过程。但我不知道如何将其作为参数传递,这不起作用。 $SqlCmd.CommandText = "执行 usp_UpdateRemoteMailbox '$PSItem.loginid'"
    猜你喜欢
    • 2011-11-10
    • 2019-10-27
    • 2021-07-25
    • 1970-01-01
    • 2014-09-30
    • 2021-11-12
    • 1970-01-01
    • 2013-06-21
    • 2011-12-31
    相关资源
    最近更新 更多