PowerShell Foreach and the pipeline



我正在尝试编辑我在网上找到的脚本。它最初是为了扫描单个用户帐户以查找chrome扩展程序而编写的。我想更改它以扫描该计算机上的计算机列表和所有用户帐户。当它扫描时,我收到扩展路径的错误。

法典:

foreach($computer in $computers){
$User = Get-Content -Path "\$computerC$Users"
Get-ChildItem $User | ForEach-Object {
Write-Host Scanning $computer

##: The extensions folder is in local appdata 
$extension_folders = Get-Content -Path "\$computerc$users$userAppDataLocalGoogleChromeUser DataDefaultExtensions"
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-01Sc$users\AppDataLocalGoogleChromeUser 
DataDefaultExtensions' because it does not exist.
At C:Usersclarkj8DesktopUntitled4.ps1:26 char:30

它没有像我希望的那样拉取用户变量。 以完成extension_folder变量所需的路径。任何帮助将不胜感激。

完整代码 大部分不是我的

$folder = "\SERVERPowershellScansExtensionList"
$hostnamestxt = "\SERVERPowershellScans404.txt"
$computers = get-content “$hostnamestxt”

if(!(Test-Path $folder)){
New-Item "\gmsms01PowershellScansExtensionListlist.txt" -type directory -force

}

Write-Host “Scanning for Extensions"

foreach($computer in $computers){
$User = Get-Content -Path "\$computerC$Users"
Get-ChildItem $User | ForEach-Object {
Write-Host Scanning $computer

##: The extensions folder is in local appdata 
$extension_folders = Get-Content -Path "\$computerc$users$userAppDataLocalGoogleChromeUser DataDefaultExtensions"
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)_localesenmessages.json" ) {
try { 
$json = Get-Content -Raw -Path "$($version_folder.FullName)_localesenmessages.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)_localesen_USmessages.json" ) {
try {
$json = Get-Content -Raw -Path "$($version_folder.FullName)_localesen_USmessages.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:SOFTWAREGoogleChromePreferenceMACsDefaultextensions.settings") {
if( Get-ItemProperty -Name "$appid" -Path "HKCU:SOFTWAREGoogleChromePreferenceMACsDefaultextensions.settings" ) {
Remove-ItemProperty -Name "$appid" -Path "HKCU:SOFTWAREGoogleChromePreferenceMACsDefaultextensions.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 "\gmsms01PowershellScansExtensionListlist.txt"
}
}
}
}
}
}

根据我上面的评论,在引发错误的行中用$_替换$user将导致您的循环按您想要的方式工作。

相关内容

最新更新