Invoke-command path null?看到这篇文章

问题描述 投票:2回答:2

我正在使用此命令,以便在我需要测试某些内容时能够远程查看和编辑注册表项到加入域上的计算机。在这种情况下,我正在看Excel的“vbawarninsg”键。这很好用。

cls
$computername = Read-Host "Enter computer name..."
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security' } | 
Select-Object PSComputerName, vbawarnings, PSParentPath | fl
$name = "vbawarnings"

下一部分是使用New-ItemProperty为“vbawarnings”键设置新值。当我为-Path名称分配变量时,它给出了一个错误“无法将参数绑定到参数'Path',因为它是null。”

这是给我一个错误的脚本

cls
$computername = Read-Host "Enter computer name..."
$registryPath = 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security'
Invoke-Command -ComputerName $computername {Get-ItemProperty -Path $registryPath } | 
Select-Object PSComputerName, vbawarnings, PSParentPath | fl
$name = "vbawarnings"

$value = Read-Host "To modify...Enter a value"
New-ItemProperty -Path $registryPath -Name $name -Value $value `
-PropertyType DWORD -Force -Verbose | Out-Null

任何帮助是极大的赞赏!

powershell
2个回答
1
投票

为了远程使用变量(例如Invoke-Command的情况),您需要使用$using:变量范围:

Invoke-Command -ComputerName $cn {
    Get-ItemProperty -Path $using:regPath
}

或将其作为参数传递:

Invoke-Command -ComputerName $cn {
    param($path)

    Get-ItemProperty -Path $path
} -ArgumentList '-path', $regPath

See this article


-1
投票

执行Invoke-Command时,该脚本块将被发送到远程服务器。

Invoke-Command -ComputerName $computername {Get-ItemProperty -Path $registryPath }

在远程服务器上,$ registryPath为null,即使您在脚本中本地使用它也是如此。

所以只需硬编码注册表路径:

Invoke-Command -ComputerName $computername {Get-ItemProperty -Path 'REGISTRY::HKEY_USERS\xxxxxxx\Software\Policies\Microsoft\office\16.0\excel\security' }
© www.soinside.com 2019 - 2024. All rights reserved.