0

PowerShell を使用してサーバーからリモート レジストリ値を取得しようとしています。

私のために働いたコードをオンラインで見つけました:

$strComputer = "remoteComputerName"    
$reg = [mcrosoft.win32.registryKey]::openRemoteBaseKey('LocalMachine',$strComputer)
$regKey = $reg.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion")
$regKey.getValue("ProgramFilesDir")

しかし、関数に入れようとすると:

$strComputer = "remoteComputerName"

function getRegValue {
    param($computerName, $strPath, $strKey)
    $reg = [mcrosoft.win32.registryKey]::openRemoteBaseKey('LocalMachine',$computerName) #Errors out here
    $regKey = $reg.OpenSubKey($strPath)
    $regKey.getValue($strKey)
}

$a = "Software\\Microsoft\\Windows\\CurrentVersion"
$b = "ProgramFilesDir"
getRegValue($strComputer, $a, $b)

エラー:

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The endpoint format is invalid."

私は何を間違っていますか?

4

2 に答える 2

3

現在の形式が問題を引き起こしているため、次のように関数を呼び出す必要があります。

getRegValue $strComputer $a $b
于 2013-03-01T16:04:54.147 に答える
1

この種の問題を回避するには、PowerShell の strictmode を使用できます。このオプションは、不適切な構文 (関数呼び出しの場合) に遭遇した場合に例外をスローします。

function someFunction{
param($a,$b,$c)
Write-host $a $b $c
}

> someFunction("param1","param2","param3")
> # Nothing happens

> Set-strictmode -version 2
> someFunction("param1","param2","param3")

The function or command was called as if it were a method. Parameters should
be separated by spaces. For information about parameters, see the
about_Parameters Help topic.
At line:1 char:1
+ someFunction("param1","param2","param3")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : StrictModeFunctionCallWithParens
于 2013-03-01T18:14:09.600 に答える