多台PC输出的测试路径不是我想要的

时间:2019-06-26 19:04:47

标签: powershell

我的地板机器的测试路径,以确保目的地为true,然后在移动文件后测试文件是否存在。我如何才能得出这个结果,以便使下半部分为真或为假?

如果我分开运行它们,则会得到输出。

PS C:\Users\amandaw.QPC> Get-Content C:\apps\currentcomputers.txt | `
   Select-Object @{Name='ComputerName';Expression={$_}},@{Name='ItemExist';Expression={ Test-Path "\\$_\c$\apps\$file"}}

ComputerName ItemExist
------------ ---------
Commercial1       True
TC-15             True
Floor03           True
Floor04           True
Mixit-PC          True
TC2018B           True
FLOOR07           True
tc2017p           True
Floor09           True
TC2017k           True
tc2017g           True
ptc-6             True
tc2017a           True
tc2017b           True
Floor15           True
tc2019z           True
tc2019b           True

# path to Item to be copied
$item = "C:\Workstation\Pictures\pngtree___material_distribution_939546_1MJ_icon.ico"
#item
$file = "pngtree___material_distribution_939546_1MJ_icon.ico"


Get-Content C:\apps\currentcomputers.txt | `
   Select-Object @{Name='ComputerName';Expression={$_}},@{Name='PathExist';Expression={ Test-Path "\\$_\c$\apps"}}

(复制不需要的文件,因为不需要帮助)

Get-Content C:\apps\currentcomputers.txt | `
   Select-Object @{Name='ComputerName';Expression={$_}},@{Name='ItemExist';Expression={ Test-Path "\\$_\c$\apps\$file"}}

输出...

PS C:\Users\amandaw.QPC> C:\Workstation\Documents\WindowsPowerShell.batch\Untitled5.ps1

ComputerName PathExist
------------ ---------
Commercial1       True
TC-15             True
Floor03           True
Floor04           True
Mixit-PC          True
TC2018B           True
FLOOR07           True
tc2017p           True
Floor09           True
TC2017k           True
tc2017g           True
ptc-6             True
tc2017a           True
tc2017b           True
Floor15           True
tc2019z           True
tc2019b           True
Commercial1           
TC-15                 
Floor03               
Floor04               
Mixit-PC              
TC2018B               
FLOOR07               
tc2017p               
Floor09               
TC2017k               
tc2017g               
ptc-6                 
tc2017a               
tc2017b               
Floor15               
tc2019z               
tc2019b

1 个答案:

答案 0 :(得分:0)

  • 我将计算机中的csv文件读取了
    (提供标题)和
  • 在两个新的(空)字段后面加上Select-Object
  • 使用Test-Path-PathType来迭代对象并填充字段
## Q:\Test\2019\06\26\SO_56779528.ps1
# path to Item to be copied
$item = Get-Item "C:\Workstation\Pictures\pngtree___material_distribution_939546_1MJ_icon.ico"
#item
$file = $item.Name

$Computers = Import-Csv C:\apps\currentcomputers.txt -Header Name |
    Select-Object *,PathExist,ItemExist

ForEach($Computer in $Computers){
   $Computer.PathExist = (Test-Path "\\$($Computer.Name)\c`$\apps" -PathType Container)

   #(copy the files- left out since don't need help with that)

   $Computer.ItemExist = (Test-Path "\\$($Computer.Name)\c`$\apps\$file" -PathType Leaf)
}

$Computers
# $Computers | Out-GridView
# $Computers | Export-Csv C:\apps\Computers.csv -NoTypeInformation

模拟样本输出:

> $Computers

Name         PathExists ItemExists
----         ---------- ----------
Commercial1        True       True
TC-15              True      False
Floor03           False      False