[英]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 :作为一般警告:
?.
?.
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.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. 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.