2017-05-03 19 views
0

私はオンラインで見つかったスクリプトを編集しようとしています。もともと、クロム拡張のために単一のユーザーアカウントをスキャンするために書かれたものです。私はそれを変更して、コンピュータの一覧とそのコンピュータ上のすべてのユーザーアカウントをスキャンします。スキャンすると拡張パスにエラーが発生します。PowerShell Foreachとパイプライン

CODE:

foreach($computer in $computers){ 

    $User = Get-Content -Path "\\$computer\C$\Users" 
    Get-ChildItem $User | ForEach-Object { 
     Write-Host Scanning $computer 


     ##: The extensions folder is in local appdata 
     $extension_folders = Get-Content -Path "\\$computer\c$\users\$user\AppData\Local\Google\Chrome\User Data\Default\Extensions\" 
     Get-ChildItem $extension_folders |ForEach-Object { 

      ##: Get the version specific folder within this extension folder 
      $version_folders = Get-ChildItem -Path "$($extension_folder.FullName)" 

       ##: Loop through the version folders found 
       foreach ($version_folder in $version_folders) { 

       ##: The extension folder name is the app id in the Chrome web store 
       $appid = $extension_folder.BaseName 

エラー:

Get-Content : Cannot find path 
'\\gms-404-01S\c$\users\\AppData\Local\Google\Chrome\User 
Data\Default\Extensions\' because it does not exist. 
At C:\Users\clarkj8\Desktop\Untitled4.ps1:26 char:30 

は、それは私が期待したように、ユーザ変数を引っ張っていません。 extension_folder変数に必要なパスを完成させます。どんな助けでも大歓迎です。

FULL CODEこれのほとんどは、あなたのループはあなたが望むように動作させるエラーを投げているラインに$_$userを交換し、上記の私のコメントパー

$folder = "\\SERVER\PowershellScans\ExtensionList\" 
$hostnamestxt = "\\SERVER\PowershellScans\404.txt" 
$computers = get-content “$hostnamestxt” 


if(!(Test-Path $folder)){ 
    New-Item "\\gmsms01\PowershellScans\ExtensionList\list.txt" -type directory -force 


} 


Write-Host “Scanning for Extensions" 



foreach($computer in $computers){ 

    $User = Get-Content -Path "\\$computer\C$\Users" 
    Get-ChildItem $User | ForEach-Object { 
     Write-Host Scanning $computer 


     ##: The extensions folder is in local appdata 
     $extension_folders = Get-Content -Path "\\$computer\c$\users\$user\AppData\Local\Google\Chrome\User Data\Default\Extensions\" 
     Get-ChildItem $extension_folders |ForEach-Object { 

      ##: Get the version specific folder within this extension folder 
      $version_folders = Get-ChildItem -Path "$($extension_folder.FullName)" 

       ##: Loop through the version folders found 
       foreach ($version_folder in $version_folders) { 

       ##: The extension folder name is the app id in the Chrome web store 
       $appid = $extension_folder.BaseName 

        ##: First check the manifest for a name 
        $name = "" 
        if((Test-Path -Path "$($version_folder.FullName)\manifest.json")) { 
         try { 
         $json = Get-Content -Raw -Path "$($version_folder.FullName)\manifest.json" | ConvertFrom-Json 
         $name = $json.name 
         } catch { 
         #$_ 
         $name = "" 
         } 
        } 

       ##: If we find _MSG_ in the manifest it's probably an app 
       if($name -like "*MSG*") { 
        ##: Sometimes the folder is en 
        if(Test-Path -Path "$($version_folder.FullName)\_locales\en\messages.json") { 
         try { 
         $json = Get-Content -Raw -Path "$($version_folder.FullName)\_locales\en\messages.json" | ConvertFrom-Json 
         $name = $json.appName.message 
         ##: Try a lot of different ways to get the name 
          if(!$name) { 
          $name = $json.extName.message 
          } 
          if(!$name) { 
          $name = $json.extensionName.message 
          } 
          if(!$name) { 
          $name = $json.app_name.message 
          } 
          if(!$name) { 
          $name = $json.application_title.message 
          } 
         } catch { 
         #$_ 
         $name = "" 
         } 
        } 
      ##: Sometimes the folder is en_US 
      if(Test-Path -Path "$($version_folder.FullName)\_locales\en_US\messages.json") { 
       try { 
        $json = Get-Content -Raw -Path "$($version_folder.FullName)\_locales\en_US\messages.json" | ConvertFrom-Json 
        $name = $json.appName.message 
        ##: Try a lot of different ways to get the name 
        if(!$name) { 
         $name = $json.extName.message 
        } 
        if(!$name) { 
         $name = $json.extensionName.message 
        } 
        if(!$name) { 
         $name = $json.app_name.message 
        } 
        if(!$name) { 
         $name = $json.application_title.message 
        } 
       } catch { 
        #$_ 
        $name = "" 
       } 
      } 
     } 

     ##: If we can't get a name from the extension use the app id instead 
     if(!$name) { 
      $name = "[$($appid)]" 
     } 

     ##: App id given on command line and this one matched it 
     if($ExtensionId -and ($appid -eq $ExtensionId)) { 
      if($Remove) { 
       echo "Removing item: [$appid] at path: [$($extension_folder.FullName)]" 
       if(!($WhatIf)) { 
        ##: Remove the extension folder 
        if (Test-Path -Path $extension_folder.FullName) { 
         Remove-Item -Path $extension_folder.FullName -Recurse -Force    
        } 

        ##: Remove the extension registry key 
        if (Test-Path -Path "HKCU:\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings") { 
         if(Get-ItemProperty -Name "$appid" -Path "HKCU:\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings") { 
          Remove-ItemProperty -Name "$appid" -Path "HKCU:\SOFTWARE\Google\Chrome\PreferenceMACs\Default\extensions.settings" 
         } 
        } 
       } 
      } else { 
       ##: Dump to a file 
       echo "Appending: [$name ($($version_folder)) - $appid] to audit file: [$auditfilepath]" 
       if(!($WhatIf)) { 
        echo "$name ($($version_folder)) - $appid" | Out-File -Append $auditfilepath 
       } 
       ##: Exit with a TRUE value if the given extension id was found 
       $retval = $true 
      } 

     ##: App id given on command line and this did NOT match it 
     } elseif($ExtensionId -and ($appid -ne $ExtensionId)) { 
      ##: NOP 
      #echo "Skipping: [$appid] output" 
     ##: App id not given on command line 
     } else { 
      ##: Dump to audit file 
      echo "Appending: [$name ($($version_folder)) - $appid] to audit file: [$auditfilepath]" 
      if(!($WhatIf)) { 
       echo "$name ($($version_folder)) - $appid" | Out-File -Append "\\gmsms01\PowershellScans\ExtensionList\list.txt" 
      } 
     } 

    } 

} 
} 
} 
+0

私はあなたがエラーがスローされている行に '$ user'の代わりに' $ _'を入れたいと思います。 –

+0

これを行ったようです。ありがとうございます –

答えて

1

を採掘されていません。

+0

これは現在 "作業中"ですが、単に拡張フォルダ以外のものをスキャンするようです。何が原因でそのエラーの原因を確認できますか?表示されるエラーはありません。最終的なファイルよりもはるかに多くを書いています。 –

+0

これは、PowerShellを完全に理解し、スクリプトを分析できることです。これはかなり深いスクリプトであり、拡張機能に関する多くの情報を探します。 "手動で"スクリプトを "実行"します。そうすることで、それが何をしているのか、そしてなぜそれが起こっているのかを見て、それを修正して、望ましくないデータの量を減らすことができます。 –

関連する問題