xml - 在看似非空的对象上编辑 Powershell 中的 XML 属性 - “您不能在空值表达式上调用方法”
问题描述
我正在尝试使用 Powershell 在我的 Wildfly Standalone.xml 中设置部署扫描器子系统的扫描间隔属性。每当我尝试对元素 (System.Xml.XmlElement) 执行任何方法时,我都会收到“您不能在空值表达式上调用方法。”,但我调用该方法的对象似乎不是无效的。
> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 407
> $Standalone="C:\Users\I10674\wildfly-9.0.2.Final\standalone\configuration\standalone.xml"
> $Standalone | Get-Member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int IComparab...
Contains Method bool Contains(string value)
CopyTo Method void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int co...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...
GetEnumerator Method System.CharEnumerator GetEnumerator(), System.Collections.IEnumerator IEnumer...
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i...
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz...
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizat...
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char padding...
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, s...
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringCom...
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime Method datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal Method decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble Method double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16 Method int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32 Method int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64 Method long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower Method string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToSByte Method sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle Method float IConvertible.ToSingle(System.IFormatProvider provider)
ToString Method string ToString(), string ToString(System.IFormatProvider provider), string I...
ToType Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider...
ToUInt16 Method uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32 Method uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64 Method uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property int Length {get;}
> [xml]$XmlContent=$(Get-Content "$Standalone")
> $XmlContent
xml server
--- ------
version="1.0" server
> $DeploymentScanner=$XmlContent.server.profile.subsystem.'deployment-scanner'
> $DeploymentScanner
name : default
path : deployments
scan-enabled : true
scan-interval : 0
relative-to : jboss.server.base.dir
runtime-failure-causes-rollback : ${jboss.deployment.scanner.rollback.on.failure:false}
> $DeploymentScanner | Get-Member
TypeName: System.Xml.XmlElement#urn:jboss:domain:deployment-scanner:2.0#deployment-scanner
Name MemberType Definition
---- ---------- ----------
ToString CodeMethod static string XmlNode(psobject instance)
AppendChild Method System.Xml.XmlNode AppendChild(System.Xml.XmlNode newChild)
Clone Method System.Xml.XmlNode Clone(), System.Object ICloneable.Clone()
CloneNode Method System.Xml.XmlNode CloneNode(bool deep)
CreateNavigator Method System.Xml.XPath.XPathNavigator CreateNavigator(), System.Xml....
Equals Method bool Equals(System.Object obj)
GetAttribute Method string GetAttribute(string name), string GetAttribute(string l...
GetAttributeNode Method System.Xml.XmlAttribute GetAttributeNode(string name), System....
GetElementsByTagName Method System.Xml.XmlNodeList GetElementsByTagName(string name), Syst...
GetEnumerator Method System.Collections.IEnumerator GetEnumerator(), System.Collect...
GetHashCode Method int GetHashCode()
GetNamespaceOfPrefix Method string GetNamespaceOfPrefix(string prefix)
GetPrefixOfNamespace Method string GetPrefixOfNamespace(string namespaceURI)
GetType Method type GetType()
HasAttribute Method bool HasAttribute(string name), bool HasAttribute(string local...
InsertAfter Method System.Xml.XmlNode InsertAfter(System.Xml.XmlNode newChild, Sy...
InsertBefore Method System.Xml.XmlNode InsertBefore(System.Xml.XmlNode newChild, S...
Normalize Method void Normalize()
PrependChild Method System.Xml.XmlNode PrependChild(System.Xml.XmlNode newChild)
RemoveAll Method void RemoveAll()
RemoveAllAttributes Method void RemoveAllAttributes()
RemoveAttribute Method void RemoveAttribute(string name), void RemoveAttribute(string...
RemoveAttributeAt Method System.Xml.XmlNode RemoveAttributeAt(int i)
RemoveAttributeNode Method System.Xml.XmlAttribute RemoveAttributeNode(System.Xml.XmlAttr...
RemoveChild Method System.Xml.XmlNode RemoveChild(System.Xml.XmlNode oldChild)
ReplaceChild Method System.Xml.XmlNode ReplaceChild(System.Xml.XmlNode newChild, S...
SelectNodes Method System.Xml.XmlNodeList SelectNodes(string xpath), System.Xml.X...
SelectSingleNode Method System.Xml.XmlNode SelectSingleNode(string xpath), System.Xml....
SetAttribute Method void SetAttribute(string name, string value), string SetAttrib...
SetAttributeNode Method System.Xml.XmlAttribute SetAttributeNode(System.Xml.XmlAttribu...
Supports Method bool Supports(string feature, string version)
WriteContentTo Method void WriteContentTo(System.Xml.XmlWriter w)
WriteTo Method void WriteTo(System.Xml.XmlWriter w)
Item ParameterizedProperty System.Xml.XmlElement Item(string name) {get;}, System.Xml.Xml...
name Property string name {get;set;}
path Property string path {get;set;}
relative-to Property string relative-to {get;set;}
runtime-failure-causes-rollback Property string runtime-failure-causes-rollback {get;set;}
scan-enabled Property string scan-enabled {get;set;}
scan-interval Property string scan-interval {get;set;}
> # Can access properties just fine
> $DeploymentScanner.'scan-interval'
0
> $DeploymentScanner.path
deployments
> # Method fails
> $DeploymentScanner.SetAttribute("scan-interval", "1000")
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $DeploymentScanner.SetAttribute("scan-interval", "1000")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
> if ($DeploymentScanner) { Write-Host "not null" } else { Write-Host "is null" }
not null
我真的很困惑当我记录 $DeploymentScanner 并将其通过管道传输到 Get-Member 时,它可以识别类并显示所有可用的方法。它甚至表明扫描间隔是属性之一。根据我最后的 if 检查,它不为空 - 为什么我会收到此错误?
解决方案
推荐阅读
- postgresql - 无法验证连接(此连接已关闭。)。可能考虑使用更短的 maxLifetime 值
- ruby-on-rails - Rails,部署到 Heroku 后:无法运行控制台,db:migrate
- firebase - 有什么方法可以在 FieldValue.arrayUnion 中添加 FieldValue.increment() 吗?
- python-3.x - 未找到 Python3 和 Docker 多阶段构建文件
- javascript - 为什么变量不会更新?
- webrtc - 浏览器如何知道他需要连接到 stun/turn 服务器?
- sql - SQL:删除两行并在表中插入新行
- reactjs - 如何在 React JSX 中使用 Stencil 的 EventEmitter?
- javascript - 检查对象数组键是否为空并将其拼接
- angular - 如何使用 Angular 上传相同的文件?