从PowerShell中的SQL SERVER存储过程中提取单个属性

问题描述 投票:0回答:1

目前我正面临这个问题:

我在SQL Server中有一个有4列的表,我使用powershell来提取数据

$connectionString = "Data Source=xx.xx.xx.xx,1433;Initial Catalog=DB;User Id=User; Password=password;"
$sqlCommand="dbo.sp_sel_hailGU"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)


$command = new-object system.data.sqlclient.sqlcommand($sqlCommand, $connection)
$command.CommandType = [System.Data.CommandType]::StoredProcedure

# $parameter = New-Object System.Data.SqlClient.SqlParameter ("@TradeDateParam", $tradeDate)
$command.Parameters.AddWithValue("@ImgName", $ImgName) | Out-Null 

$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command

$connection.Open()   
$adapter.Fill($result) | Out-Null
$connection.Close()

return $result.Tables

此代码以下列格式显示表中的所有值

attribute1: value1
attribute2: value22
attribute3: value33
attribute4: value44

attribute1: value2
attribute2: value222
attribute3: value333
attribute4: value444

但是,我想在Powershell中做的是以单独的方式获取每个属性的值并将它们分配给Powershell变量以便进行一些数据处理

我试图为变量创建一个管道尝试获取属性,但无济于事

任何帮助将不胜感激

问候

sql-server powershell stored-procedures
1个回答
0
投票

您可以使用点表示法输出对象的值。

$var.Access.IdentityReference

或这个..

$($var.Access).IdentityReference

至于......

以单独的方式获取每个属性的值...

这是什么意思?

如果你的意思是将这两个价值项彼此分开......

attribute1: value1
attribute2: value22
attribute3: value33
attribute4: value44

attribute1: value2
attribute2: value222
attribute3: value333
attribute4: value444

...

你可以拆分':'并选择你想要的正确位置。

$DBValues = @'
    attribute1: value1
    attribute2: value22
    attribute3: value33
    attribute4: value44

    attribute1: value2
    attribute2: value222
    attribute3: value333
    attribute4: value444
'@

$DBValues -split ': '

或者使用RegEx选择值...示例:

# Grab the string value based on the match
[regex]::Matches($DBValues,'\svalue.*').Value

# Grab the string value after the match
[regex]::Matches($DBValues,'(?<=attribute.*[:]\s).*?(?=\s)').Value

至于......

将它们分配给Powershell变量

...除非您在脚本中设置了数据,否则无法传输数据

param
(
  [Parameter(mandatory=$true, ValueFromPipeline=$true)]$InputObject,
  [Parameter(mandatory=$true)]$FileName
)
...
© www.soinside.com 2019 - 2024. All rights reserved.