【发布时间】:2021-02-04 19:21:32
【问题描述】:
在 Powershell 中,我正在对单行数据进行 SQL 查询。以 $data 为例。
来自 System.Data.DataSet 类型的查询的响应。在其中,有一个 tables 属性,其中包含我需要的数据。
$data.Tables
ServerName : Server15
SamAccount : Admin-Server15
LastPWDReset : 1/15/2019 12:00:00 AM
LastPWDResetAttempt :
我不打算用任何东西写回这些数据。相反,我想显示它,并将空的“LastPWDResetAttemp”转换为空白的“NONE”。
我以为会这样:
$data.Tables.lastPWDResetAttempt = "None"
但这给了我一个错误在这个对象上找不到属性“lastPWDResetAttempt”。验证该属性是否存在并且可以设置。
我忍不住想我错过了从“数据集”到“字符串”的一些转换。
我尝试过 to-string,但这样做时,我得到的只是一串数据,而不是标题。我无法更新或轻松使用任何内容来构建我的最终表格。
我的解决方法:
$webdata = "" | select ServerName,SamAccount,LastPWDReset,LastPWDResetAttempt
$webdata.ServerName = $data.tables.servername
$webdata.SamAccount = $data.tables.samaccount
$webdata.LastPWDReset = $data.tables.LastPWDReset
$webdata.LastPWDResetAttempt = $data.tables.LastPWDResetAttempt
$webdata.LastPWDResetAttempt = "Never"
有效。我简直不敢相信没有更简单的方法,我也不明白为什么我可以查看数据列表,只是无法设置它。
【问题讨论】:
-
$data.Tables[0].lastPWDResetAttempt = "None" 给了我相同的“在此对象上找不到属性‘lastPWDResetAttempt’。验证该属性是否存在并且可以设置。”如果我尝试查看它是空的,并且类型为“System.DBNull”
-
这似乎是一个已知问题,我越看.. DBNull 行为很奇怪!为什么它的行为不符合预期? 在一个网站上找到,其中提到了关闭的 Microsoft Connect 网站。几个关于检查 NULL 的对话 - stackoverflow.com/questions/22285149/… 但没有关于能够设置值的内容。
标签: powershell dbnull