【问题标题】:Powershell null-valued expressionPowershell 空值表达式
【发布时间】:2012-06-21 15:51:00
【问题描述】:

我有下面的脚本,我一生都无法理解为什么它给了我“你不能在空值表达式上调用方法”。它在两个地方出错。

Which computer?: NFDW2206
What is the AssetID?: 00000007
Checking NFDW2206 to see if the Registry Key exists..
You cannot call a method on a null-valued expression.
At \\NFDNT007\Dept\Corporate\IT\Network Services\Documentation\Asset Tag.ps1:11 char:33
+         $regassetid = $regKey.GetValue <<<< ("AssetID")
    + CategoryInfo          : InvalidOperation: (GetValue:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The Key does not exist. Writing AssetID.....
You cannot call a method on a null-valued expression.
At \\NFDNT007\Dept\Corporate\IT\Network Services\Documentation\Asset Tag.ps1:18 char:20
+             $regKey.Setvalue <<<< ('AssetID', $AssetID, 'String')
    + CategoryInfo          : InvalidOperation: (Setvalue:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

代码如下

$Computer = Read-Host "Which computer?"
$AssetID = Read-Host "What is the AssetID?"

if (($Computer -eq "") -or ($AssetID -eq "")) {
    Write-Host "Error: A blank parameter was detected" -BackgroundColor Black -ForegroundColor Yellow
} else {
    if (Test-Connection -comp $Computer -count 1 -quiet) {
        Write-Host "Checking $Computer to see if the Registry Key exists.."
        $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $Computer)
        $regKey = $reg.OpenSubKey("SOFTWARE\Multek Northfield")
        $regassetid = $regKey.GetValue("AssetID")

        if ($regassetid -eq $null) {

            Write-Host "The Key does not exist. Writing AssetID....."
            $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $Computer)
            $regKey = $reg.OpenSubKey("SOFTWARE\Multek Northfield",$True) ## $True = Write
            $regKey.Setvalue('AssetID', $AssetID, 'String')

        } else {

            $OverWrite = Read-Host "AssetID exists do you wish to continue?"

            if (($OverWrite -eq "y") -or ($OverWrite -eq "yes")) {
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer)
                $regKey = $reg.OpenSubKey("SOFTWARE\Multek Northfield",$True) ## $True = Write
                $regKey.Setvalue("AssetID", $AssetID, "String")
            }

        }
    } else {
        Write-Host "Error: $computer is offline..." -BackgroundColor Black -ForegroundColor Yellow
    }
}

【问题讨论】:

  • 为什么不使用 PowerShell 注册表提供程序? blogs.technet.com/b/heyscriptingguy/archive/2012/05/07/…
  • 我读过的所有内容都说您需要打开远程处理才能以任何其他方式执行此操作,并且当前打开远程处理不是一种选择。
  • 那些提供者早于远程处理。

标签: variables powershell


【解决方案1】:

文档说如果操作失败,OpenSubKey 将返回 null。很可能是找不到钥匙。远程系统是 64 位操作系统吗?如果是这样,则可能是您遇到了注册表虚拟化问题。可能需要看下SOFTWARE\WOW6432Node\Multek Northfield

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多