0

リモート レジストリで文字列を読み取ろうとしています。作業中のスクリプトを実行すると、リスト内のワークステーションに接続されますが、リモートではなく、実行時にローカル コンピューターのみが読み取られます。何か案は?

#create open dialog box
Function Get-FileName($initialDirectory)
{
    [void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' );
    $d = New-Object Windows.Forms.OpenFileDialog;
    $d.ShowHelp = $True;
    $d.filter = "Comma Separated Value (*.csv)| *.csv";
    $d.ShowDialog( ) | Out-Null;
    $d.filename;
}

# Set Variables with arguments
$strFile = Get-FileName;
$strComputer = Get-Content $strFile;
$date = Get-Date -Format "MM-dd-yyyy";
$outputFile = "C:\PowerShell\Reports";

$cred = Get-Credential

foreach($computer in $strComputer)
{
Enter-PSSession $computer -Credential $cred
Set-Location HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability
$systemInfo = Get-Item -Name LastComputerName
Write-Host $systemInfo
}
4

2 に答える 2

2

コメントが既に説明したように、Enter-PSSessionインタラクティブな使用のためです。リモート レジストリ エントリを読み取るには、いくつかの方法があります。

プレーンreg.exeを使用すると、十分に機能します。そのようです、

foreach($computer in $strComputers) {
  reg query \\$computer\hklm\software\Microsoft\Windows\CurrentVersion\Reliability  /v LastComputerName
}

PSSession を使用します。セッションを作成し、Invoke-Commandレジストリを読み取ります。そのようです、

function GetRegistryValues {
  param($rpath, $ivalue)
  Set-Location $rpath
  $systemInfo = (Get-ItemProperty .).$ivalue
  Write-Host $systemInfo
}
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session -Scriptblock ${function:GetRegistryValues} `
 -argumentlist "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability",`
 "LastComputerName"
Remove-PSSession $session

.Net クラス、Microsoft.Win32.RegistryKey を使用します。そのようです、

$sk = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $server)
$k = $sk.opensubkey("SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability", $false)
write-host $k.getvalue("LastComputerName")
于 2013-06-15T05:45:54.630 に答える