2017-02-01 3 views
2

私はpowershellでかなり新しいです。私はcsvファイル内のPCと離れてリモートでレジストリキーの状態を抽出しようとしています。しかし、私はエラーにpowershell invoke-commandでの問題、式や文での予期しないトークン '-ArgumentList'

"Unexpected token '-ArgumentList' in expression or statement." when I try to execute. And I am not pretty sure on the syntax of the Invoke-Command, can one of your pls verify if it is correct? Appreciate your help. 

を取得するので、基本的に、私は何をするつもりは、私は、その後呼び出しコマンド内で使用される出力パスを指定して、コンピュータ名を取得し、です。 PCのオンライン状態をテストし、必要なレジストリ値をチェックし、ファイルに書き込む。あなたのエラーを修正するには

$computers = Get-Content "C:\Temp\AutoSug\Computers.txt" 

$output_path = "C:\Temp\AutoSug\output.csv" 

foreach($computer in $computers) 
{ 
    Test-Connection -computername $computer -Quiet 
    If (Test-Connection $computer -count 1 -quiet) 
    { 
     Invoke-Command -computer $computer -ScriptBlock 
     { 
      param(
       $output_path 
      ) 
      $hostname = (Get-CIMInstance CIM_ComputerSystem).Name 
      $objExcel = New-Object -ComObject Excel.Application 
      if ($objExcel.Version -eq "15.0") 
      { 
       $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
       $Users = @() 
       $value = @() 
       foreach ($User in $HKEY_USERS) 
       { 
        $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" } 
        $SID = $PROFILESID.PSChildName 
        foreach ($value in $SID) 
        { 
         $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\15.0\Outlook\Preferences -ErrorAction SilentlyContinue 
         $gold = $key.property 
         if($gold -like 'ShowAutoSug') 
         { 
          $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\15.0\Outlook\Preferences).ShowAutoSug 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname, $objUser, $value , $grail | Add-Content $output_path 
         } 
         else 
         { 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname,$objUser, $value , "The Auto Complete is not disabled" | Add-Content $output_path 
         } 
        } 
       } 
      } 

      if ($objExcel.Version -eq "14.0") 
      { 
       $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
       $Users = @() 
       $value = @() 
       foreach ($User in $HKEY_USERS) 
       { 
        $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" } 
        $SID = $PROFILESID.PSChildName 
        foreach ($value in $SID) 
        { 
         $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\14.0\Outlook\Preferences -ErrorAction SilentlyContinue 
         $gold = $key.property 
         if($gold -like 'ShowAutoSug') 
         { 
          $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\14.0\Outlook\Preferences).ShowAutoSug 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname, $objUser, $value , $grail | Add-Content -path $output_path 
         } 
         else 
         { 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname,$objUser, $value , "The Auto Complete is not disabled" | Add-Content $output_path 
         } 
        } 
       } 
      } 

      if ($objExcel.Version -eq "12.0") 
      { 
       $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
       $Users = @() 
       $value = @() 
       foreach ($User in $HKEY_USERS) 
       { 
        $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" } 
        $SID = $PROFILESID.PSChildName 
        foreach ($value in $SID) 
        { 
         $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences -ErrorAction SilentlyContinue 
         $gold = $key.property 
         if($gold -like 'ShowAutoSug') 
         { 
          $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences).ShowAutoSug 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname, $objUser, $value , $grail | Add-Content $output_path 
         } 
         else 
         { 
          $objSID = New-Object System.Security.Principal.SecurityIdentifier($value) 
          $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
          $hostname,$objUser, $value , "The Auto Complete is not disabled" |Add-Content $output_path 
         } 
        } 
       } 
      } 
     } -ArgumentList $output_path 
    } 
    else 
    { 
     $status = 'Offline' 
     $computer , $status | Add-Content $output_path 
    } 
} 
+0

。あなたの問題を詳述し、[最小、完全で、かつ証明可能な例](http://stackoverflow.com/help/mcve) –

答えて

1

は、単に-ArgumentList $output_pathをカットし、同じよう-ScriptBlock前にそれを置く:それはあなたの問題があると何が求めているものを、明確ではありません

Invoke-Command -computer $computer -ArgumentList $output_path -ScriptBlock .... 
+0

を追加してください。リモートレジストリをPSDriveとしてマッピングすると、コードがきれいになり、 'Invoke-Command'をまったく使用する必要もありません。 – Fairy

+1

@halfer THX、そうですよ! –

関連する問題