Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Write Verbose如何从Invoke Sqlcmd生成记录数?_Powershell - Fatal编程技术网

Powershell Write Verbose如何从Invoke Sqlcmd生成记录数?

Powershell Write Verbose如何从Invoke Sqlcmd生成记录数?,powershell,Powershell,我想从调用Sqlcmd生成记录数。它返回一个[System.Data.DataRow]数组长度和计数未在获取成员中提及,但它们似乎可以工作并且是[int32]。如何获取要显示的记录数 $Results = Invoke-Sqlcmd -Query $Query -ServerInstance $Server -ErrorAction SilentlyContinue Write-Verbose -Message $($Results.Length.ToString()) # fails

我想从调用Sqlcmd生成记录数。它返回一个[System.Data.DataRow]数组<代码>长度和
计数
未在
获取成员
中提及,但它们似乎可以工作并且是
[int32]
。如何获取要显示的记录数

$Results = Invoke-Sqlcmd -Query $Query -ServerInstance $Server -ErrorAction SilentlyContinue

Write-Verbose -Message $($Results.Length.ToString())    # fails
Write-Verbose -Message "$($Results.Length.ToString())"  # fails
$xxx = 7
Write-Verbose -Message $xxx.ToString()                  # works
从控制台上看,这些类型看起来应该可以工作

PS C:\> $Result = Invoke-Sqlcmd -Query "SELECT TOP 3 TABLE_NAME FROM INFORMATION_SCHEMA.TABLES;" -ServerInstance DEVDW02

>> $Result.GetType()
>> $Result.Length
>> $Result.Length.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
3
True     True     Int32                                    System.ValueType
更新:

工作内容: PS C:>$PSVersionTable.PSVersion.ToString() 7.1.3

失败于: PS C:>$PSVersionTable.PSVersion.ToString() 5.1.17763.1490

You cannot call a method on a null-valued expression.
At C:\Users\lit\Documents\WindowsPowerShell\Modules\WmEdwUtils\1.0.0.5\Get-SqlAgentJobRun.psm1:119 char:38
+             Write-Verbose -Message $($Results.Length.ToString())
+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

由于您使用的是
Write Verbose
,我猜您需要将
$VerbosePreference
设置为
Continue
或使用
Write Verbose
cmdlet指定
-Verbose
。我在命令行上使用的是
-Verbose
。7按预期显示。我在您的代码中没有看到它。在任何情况下,如果我在调用
write Verbose
之前写入
write Verbose$($Results.Count.ToString())-Verbose
,或者将
$VerbosePreference
设置为
Continue
,那么它对我都有效。您使用的是哪个版本的PowerShell<代码>$PSVersionTable.PSVersion.ToString()我在服务器2012 R2上的Windows PowerShell 5.1.14409.1018上进行了测试。我无法重现这个问题;很抱歉