Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables Powershell空值表达式_Variables_Powershell - Fatal编程技术网

Variables Powershell空值表达式

Variables Powershell空值表达式,variables,powershell,Variables,Powershell,我有下面的脚本,我一辈子都不明白为什么它会给我“你不能对空值表达式调用一个方法”。它有两个错误 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\Netwo

我有下面的脚本,我一辈子都不明白为什么它会给我“你不能对空值表达式调用一个方法”。它有两个错误

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

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

为什么不使用PowerShell注册表提供程序?我读到的所有内容都说,您需要打开远程处理才能以任何其他方式执行此操作,而当前打开远程处理不是一个选项。这些提供商早于远程处理。
$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
    }
}