2015-12-21 56 views
0

我在PowerShell腳本中遇到了一些麻煩。這樣做的目的是蜘蛛網絡,並尋找任何PC上存在的文件/文件夾。

這裏是原始來源:

#FiToFin Script# 
$Fltr = "how_recover*.*" 
$Online = "C:\Users\<username>\Scripts\Logs\Online.log" 
$CSV = "C:\Users\<username>\Scripts\Devices.csv" 
#$Tstpath = test-path "\\$computer\c$" 
$Offline = "C:\Users\<username>\Scripts\Logs\Offline.log" 
################################################## 
$devices = Get-Content "$CSV" 
foreach ($computer in $devices) { 
    Test-Path "\\$computer\c$" > $Tstpath 
    if ($Tstpath -eq $True) { 
    ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse | 
     Out-File -Append $Online 
    } else { 
    Write-Host "$computer is NOT Online" | Out-File -Append $Offline 
    } 
} 
################################################## 
Write-Host "_____________________" 
Write-Host "Online file = $Online" 
Write-Host "Offile file = $Offline" 
Write-Host "_____________________" 

我已經改變了if聲明if($Tstpath -eq "True")if ($lastexitcode -eq $true)if($Tstpath -eq $false),他們都只是解析第一個{Do command}不管。他們永遠不會掉入else。甚至試圖將Tstpath = Test-Path \\$computer\c$作爲一個變量,然後運行它。

當解析第一個{Do Command}回報是

ls : Cannot find path '\\<computerName>\c$\u' because it does not exist. 
At C:\Users\<username>\Scripts\FiToFin.ps1:19 char:3 
+   ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $On ... 
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (\\<computername>\c$\u:String) [Get-ChildItem], ItemNotFoundException 
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand 

什麼工作:

如果我的測試機器上,我可以ls -Path "\\$computer\c$\users" -Filter $Fltr -Recurse | Out-File -Append $Online就好了。

我從Test-Path \\$computer\c$TrueFalse和甚至可以> $varWrite-Host結果就好了。

我不知道這是爲什麼,並希望知道。

這也適用於:

################################################################### 
$computer = "TestPC" 
$Tstpath = Test-Path \\$computer\c$ 
#################################################################### 
$Tstpath > $null 
if($Tstpath -eq $True) { 
    Write-Host "$computer is Online" 
} else { 
    Write-Host "$computer is NOT Online" 
} 

但是當你添加的命令lsGet-ChildItem它怪胎。 所以,問題是:爲什麼它永遠不會執行else部分?

+1

爲什麼還要保存結果呢?你有幾個處理問題,但主要是'$ Tstpath'的值永遠不會改變。 'if(test-path「\\ $ computer \ c $」){}' – Matt

+0

保存結果可以看到文件在〜2k機器上的位置。爲什麼它沒有改變?底部工作的很好,但是當你把'ls'放在'$ true'上時,它立即剎車。 – Benny

+1

我的意思是'test-path'的結果。如果你的成功,那麼你知道它通過與否。我通過跳過一些冗餘邏輯來幫助你。只需將測試放在'if'中 – Matt

回答

3

我看到兩個問題會導致您的問題。你如何初始化和更新變量$Tstpath

# Presumably Initialize 
$Tstpath = test-path "\\$computer\c$" 

# Updating in loop 
test-path "\\$computer\c$" > $Tstpath 

我會認爲你是在PowerShell的ISE測試和$Tstpath不得不在某些時候$真正的價值。

問題是你是從來沒有更新變量。縱觀TechNet瞭解about_redirection你會發現:

Operator Description    Example 
    -------- ---------------------- ------------------------------ 
    '>'  Sends output to the  Get-Process > Process.txt 
       specified file. 

你的命令,試圖輸出爲「文件」。你應該有一個關於無法找到文件或文件在你的系統中的單個布爾值的錯誤(因爲它不是一個附加的重定向器)的錯誤。

你應該做什麼來保持你的邏輯是通過分配保存結果。

$Tstpath = Test-path "\\$computer\c$" 

然後你可以測試一下。

然而這是多餘的,因爲你再也不需要這個值了。如果將它直接放在if語句中會更容易。

if(test-path "\\$computer\c$"){"Do something"}else{"Fail Trumpet"} 

我也建議使用Export-CSV -Append因爲你正在處理的對象。會產生良好的結構化輸出。

Get-ChildItem -path "\\$computer\c$\users\" -Filter $Fltr -Recurse | Export-CSV -Append $Online -NoTypeInformation