简体   繁体   English

powershell 脚本请帮助 - “您不能在空值表达式上调用方法。”

[英]powershell script help please - "You cannot call a method on a null-valued expression."

Here is the script for reference - it is failing at the below line and I'm not sure why - appreciate any help:)这是供参考的脚本-它在下面一行失败了,我不知道为什么-感谢任何帮助:)

You cannot call a method on a null-valued expression.
At C:\users\jk\documents\jk.ps1:19 char:45
 ... -VMhost | %{$_.ExtensionData.UpdateProductLockerLocation($datastore)} ...                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

script :脚本

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
# VMware VirtualCenter server name 
$VCserver = Read-Host "Enter the vCenter server name"

# Connect to the vCenter server 
Connect-VIServer -server $VCserver

# variable
$cluster = 'cluster01'
$hosts = Get-Cluster -Name $cluster | Get-VMHost
$datastore = '/vmfs/volumes/04c62043-2d22f726/vmwtools/VMware Tools 12.0.6/'

# Display the current VMware Tools location
# default is: /locker/packages/vmtoolsRepo/
$hosts | Get-AdvancedSetting -Name "UserVars.ProductLockerLocation" | Select-Object Entity, Value

# Change the VMware Tools location
Get-cluster -name $cluster | Get-VMhost | %{$_.ExtensionData.UpdateProductLockerLocation($datastore)}  

# Display current VMware Location
$hosts | Get-AdvancedSetting -Name "UserVars.ProductLockerLocation" | Select-Object Entity,Value

# Disconnect from vCenter 
Disconnect-VIserver -server * -Confirm:$false

As the wording of the error message suggests ("You cannot call a method on a null-valued expression."), you tried to call a method on an expression that is effectively $null , which in your case is $_.ExtensionData .正如错误消息的措辞所暗示的那样(“您不能在空值表达式上调用方法。”),您尝试在有效$null的表达式上调用方法,在您的情况下为$_.ExtensionData

If that expression is expected to be situationally $null in your ForEach-Object ( % ) call, you can simply avoid calling the method whenever you find the expression value to be $null :如果该表达式在您的ForEach-Object ( % ) 调用预期$null ,则只要发现表达式值为$null ,您就可以简单地避免调用该方法

In PowerShell (Core) 7.1 and above , you can use the null-conditional operator , ?.PowerShell (Core) 7.1 及以上版本中,可以使用空条件运算符, ?. :

# PS v7.1+
# Note the use of ?. instead of .
Get-cluster -name $cluster | Get-VMhost |
  ForEach-Object { $_.ExtensionData?.UpdateProductLockerLocation($datastore) } 

As a general caveat :作为一般警告

  • While ?.?. works as expected with properties , with variables it unfortunately and unexpectedly requires the variable name to be enclosed in {...} , because - surprisingly - ?使用属性按预期工作,不幸的是,它意外地要求变量名包含在{...}中,因为 - 令人惊讶 - ? is a legal character in a variable name.是变量名中的合法字符。
  • For instance, Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper()例如, Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper() Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper() Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper() works, but using $foo?.ToUpper() instead does not , because PowerShell looks for a variable named $foo? Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper()有效,但使用$foo?.ToUpper()无效,因为 PowerShell 查找名为$foo? (sic), which doesn't exist. (原文如此),它不存在。 Requesting that this counterintuitive behavior be changed - even though it is technically a breaking change - is the subject of GitHub issue #14025 .要求改变这种违反直觉的行为——即使它在技术上是一个重大变化——是GitHub 问题 #14025的主题。

In Windows PowerShell you can use an if statement:Windows PowerShell 中,您可以使用if语句:

Get-cluster -name $cluster | Get-VMhost |
  ForEach-Object { 
    if ($null -ne $_.ExtensionData)  { 
      $_.ExtensionData.UpdateProductLockerLocation($datastore) 
    } 
  }

Note how $null is placed on the LHS (left-hand side) of the -ne operation above (applies equally to -eq ), which is a good habit to form, because it is the only fully reliable way to test for $null - see the docs .注意$null是如何放在上面-ne操作的LHS (左侧)的(同样适用于-eq ),这是一个好习惯,因为它是测试$null的唯一完全可靠的方法- 查看文档

暂无
暂无

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

相关问题 您不能对空值表达式调用方法。 Powershell - You cannot call a method on a null-valued expression. Powershell 您不能在空值表达式上调用方法。 通过 powershell 在网页中单击按钮 - You cannot call a method on a null-valued expression. clicking button in webpage via powershell 您不能在空值表达式上调用方法。 Powershell 脚本 - You cannot call a method on a null-valued expression. Powershell scripting 您不能使用 excel 在 powershell 中调用空值表达式的方法 - You cannot call a method on a null-valued expression in powershell with excel Powershell给我“您不能在空值表达式上调用方法。”但工作原理不一致。 到底是怎么回事? - Powershell gives me “You cannot call a method on a null-valued expression.” but works - inconsistently. What is going on? powershell '您不能在空值表达式上调用方法' - powershell 'You cannot call a method on a null-valued expression' Powershell:您不能在空值表达式上调用方法 - Powershell: You cannot call a method on a null-valued expression Powershell:您不能在空值表达式上调用方法 - Powershell : You cannot call a method on a null-valued expression 是什么导致错误“您不能在空值表达式上调用方法。” - What is causing the error “You cannot call a method on a null-valued expression.” 尝试接收在后台作业中运行的更新搜索器的结果时,“您不能在空值表达式上调用方法。” - “You cannot call a method on a null-valued expression.” when trying to receive Result of update Searcher running in Background Job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM