2015-12-22 72 views
0

我正在为此苦苦挣扎几个小时,我尝试Google,但使用双引号,Invoke-Expression或者使用@缩写无济于事。在PowerShell命令行中使用包含引号的变量

$SrcLoc = '' 
$VMachine = 'computer' 
$Paths = @() 
$Paths = ('\c$\Users\', '\c$\Program Files\', '\c$\Program Files (x86)\') 

foreach ($path in $Paths){ 
    $SrcLoc += '"\\' + $VMachine + $path + '" ,' 
} 
$SrcLoc = $SrcLoc.Substring(0,$SrcLoc.Length-2) 
#$CREDENTIALED_SECTION = @{Username=$SrcLoc} 
Write-Host $SrcLoc 
#Invoke-Expression $SrcLoc 
Get-ChildItem $SrcLoc -filter "*google*" -Directory -Recurse -force | % { $_.fullname } 

,其结果是:

PS C:\Users\admin> C:\Users\admin\Desktop\New folder (3)\chrome2.ps1 
"\\computer\c$\Users\" ,"\\computer\c$\Program Files\" ,"\\computer\c$\Program Files (x86)\" 
\\computer\c$\Users\ 
\\computer\c$\Program Files\ 
\\computer\c$\Program Files (x86)\ 

Get-ChildItem : Illegal characters in path. At C:\Users\admin\Desktop\New folder (3)\chrome2.ps1:13 char:1 
+ Get-ChildItem $SrcLoc -filter "*google*" -Directory -Recurse -force | % { $_.f ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [Get-ChildItem], ArgumentException 
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand 

请告知 谢谢

+0

这里的目标是什么?帮助我理解你正在努力完成的任务。 – FoxDeploy

+0

我想在整个环境中找到所有Google chrome setup.exe文件,以便远程卸载它。 现在$ VMachine只是一个字符串,但我想将它用作未来的数组,以遍历整个环境,搜索和销毁。 – Michal

回答

3

我只是有些感动周围的事物,并清理语法一点,它工作得很好:)

事实证明,你可以忘掉所有这些花哨的格式,并且让自己更容易,所以在你想要提出的$SrlLoc,只需将该语句移入ForEach循环,并在那里获取目录内容。无需多个步骤,你不需要建立一个空数组项目添加到它(这是你与$srcLoc = @()在做什么)

$VMachine = 'computer' 
$Paths = ('c$\Users\', 'c$\Program Files\', 'c$\Program Files (x86)\') 

foreach ($path in $Paths){ 
    $SrcLoc = "\\$VMachine\$path" 
    Write-Output "Searching $srcloc" 
    Get-ChildItem $SrcLoc -filter "*google*" -Directory -Recurse -ea SilentlyContinue | 
    select -ExpandProperty FullName 
} 

我搬到你的Get-ChildItem命令成foreach循环,让事情变得更清洁,更容易理解。至于变量contatenation的语法,我真的建议人们避开$something = "sometext" + $SomeObject + (Some-Command).Output,当你要求PowerShell将这些事情拼凑在一起时,你正在求助于错误。

代码的输出是这样的。

Searching \\behemoth\c$\Users\ 
\\behemoth\c$\Users\Stephen\Dropbox\My Code\SCCMBackup\Google Earth 
Searching \\behemoth\c$\Program Files\ 
Searching \\behemoth\c$\Program Files (x86)\ 
\\behemoth\c$\Program Files (x86)\Google 
\\behemoth\c$\Program Files (x86)\Microsoft SDKs\Microsoft Azure\Mobile Services\2.0\Packages\Microsoft.Owin.Security.Google.2.1.0 
\\behemoth\c$\Program Files (x86)\Plex\Plex Media Server\Resources\Plug-ins-4ccd2ca\Services.bundle\Contents\Service Sets\com.plexapp.plugins.googledrive 
\\behemoth\c$\Program Files (x86)\Plex\Plex Media Server\Resources\Plug-ins-4ccd2ca\Services.bundle\Contents\Service Sets\com.plexapp.plugins.googledrive\URL\Google Drive 
\\behemoth\c$\Program Files (x86)\TechSmith\Camtasia Studio 8\GoogleDrive 

我加-ErrorAction SilentlyContinueGet-ChildItem,因为你会遇到权限问题,试图在受保护的目录中查找远程。使用-Ea SilentlyContinue可以抑制这些错误。

+0

它看起来好多了。不幸的是它不起作用。我想知道它为什么适用于你,而不是我。 _Get-ChildItem \\ lonvdysand \ c $ \ Users \ -filter“* google *”-Directory -Recurse -force -ea SilentlyContinue | %{$ _。fullname} _ ** \\ lonvdysand \ c $ \ Users \ YSand \ AppData \ Local \ Google \\ lonvdysand \ c $ \ Users \ YSand \ AppData \ Local \ Google \ Chrome \ User Data \ Default \ databases \ https_www.google.com_0 ** _PS C:\ Users \ mhoffman> Get-ChildItem \\ lonvdysand \ c $ \ Users \ -filter“* google *”-Directory -Recurse -ea SilentlyContinue |选择-ExpandProperty FullName_ 这不会返回:( 谢谢 – Michal

+0

请确保您的名称中包含Google的某些目录:)。你也可以删除-errorAction SilentlyContinue,看看是否有错误给你提供线索。 – FoxDeploy

+0

是的,我没有删除,没有错误:/ 是的目录是有第一个命令发现它们 – Michal

相关问题