我应该因为sqlcmd
上没有安装数据库执行远程SQL命令。
$Configuration = Invoke-Command -ComputerName $dataserverName -Credential $cred -ScriptBlock {
sqlcmd.exe -S $using:dataserver -U $using:user -P $using:pass -d $using:database -q $using:SQLQuery
}
所以,当我执行我的要求,我在同一个块的响应,所以我不能这样从每一列获取值:
MinimumSize MaximumSize Delimiter IdFile -------------------- -------------------- --------- ---------------------- NULL NULL | 6 (1 rows affected)
所以,如果我做$Configuration[0]
,我
$Configuration[0] = MinimumSize MaximumSize Delimiter IdFile
像一条线,所以我不能做任何事情。我的问题是如何获得例如$Configuration.IdFile = 6
。
我也一直在系统中调用,SQLCMD不存在,并不允许安装软件。 SQLCMD.exe返回表示每行字符串数组返回,而不是像Invoke-Sqlcmd对象呢,所以你必须解析$配置[2]。
但这里有一个窍门:改变SQL语句返回XML,而不是一个表,你可以让Powershell的做解析为您服务。你没有提供的SQL,但它应该是很容易适应这个以满足您的需求:
$SQL = @"
SET NOCOUNT ON
DECLARE @XML AS XML
SET @XML = (--change this SELECT statement to match yours, but keep the 'AS Row' table alias:
SELECT Name, recovery_model_desc
FROM master.sys.databases AS Row
FOR XML AUTO, ROOT('table') -- also keep this line
)
SELECT CAST (@XML AS nvarchar(max)) -- do this to prevent SQLCMD adding line breaks
"@
[string] $output = (SQLCMD -S 'YOUR_SQL_SERVER' -E -h-1 -y0 -Q $SQL)
([xml] $output).table.Row[0].Name #now you have an array of rows and can reference each field
([xml] $output).table.Row | Out-GridView #you can also put all the rows into a GridView!