简体   繁体   English

PowerShell 给出错误“对象引用未设置到实例对象”

[英]PowerShell giving an error "Object reference not set to an instance object"

Am receiving an error from my powershell script everytime it runs.每次运行时,我的 powershell 脚本都会收到错误消息。 However, it does run successfully.但是,它确实运行成功。 I would like to know what is causing the error message.我想知道是什么导致了错误消息。 Thanks!谢谢!

PowerShell:电源外壳:

 [xml]$file = get-content "c:\Windows\personalsettings\PersonalSettings.xml"
  $xmlProperties = $file.SelectNodes("/Objects/Object/Property")
   If ($xmlProperties.Where({ $_.InnerText -eq 'FireFox' }, 'First'))
    {
    Invoke-expression "C:\Windows\SetDefaultBrowser\setdefaultbrowser"
  }
   If ($xmlProperties.Where({ $_.InnerText -eq 'Chrome' }, 'First'))
  { 
  Invoke-expression "C:\Windows\psu\protected\SetDefaultBrowser\setdefaultbrowser"
 }

XML File: XML文件:

<?xml version="1.0"?>
<Objects>
<Object>
<Property Name="Browser">Firefox</Property>
<Property Name="PDF">Adobe Reader</Property>
</Object>
</Objects>

The xml file is declared with a namespace <?xml version="1.0"?> and that's why it is not selecting the nodes. xml 文件使用命名空间<?xml version="1.0"?> ,这就是它不选择节点的原因。 It will be better to get the values like ordinary objects:像普通对象一样获取值会更好:

$file = [xml](Get-Content "c:\Windows\personalsettings\PersonalSettings.xml")
$file.objects.object.property | ForEach {
  If ($_.Innertext -eq "Firefox") {
    Invoke-expression "C:\Windows\SetDefaultBrowser\setdefaultbrowser"
  }
  If ($_.Innertext -eq "Chrome") {
    Invoke-expression "C:\Windows\psu\protected\SetDefaultBrowser\setdefaultbrowser"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM