PowerShell - Test-path -
i have script gets drives on host , outputs text file. text file below -
d: e: f: g: i need test each drive path , if true assign variable.
i have following far not working. know super obvious, appreciated.
get-wmiobject win32_logicaldisk -filter "drivetype=3 , deviceid!='c:'" | select deviceid | format-table -hidetableheaders > c:\deviceid.txt -force $deviceid = get-content c:\deviceid.txt $deviceid | foreach {$_.trimend()} | ? {$_.trim() -ne '' } > c:\deviceid.txt $deviceid = get-content c:\deviceid.txt $path = "$deviceid\apps\netprobent\" $pathexists = test-path $path foreach-object ($deviceid in $deviceid) { if ($pathexists -eq $true) { $devicedrive = $deviceid} else { $devicedrive = "c:"} } if need more information please let me know.
thanks below working.
i have come across method works
$folder = "apps\netprobent" get-psdrive | where-object { $_.root -match "[c-z]:\\" -and (test-path $(join-path $_.root $folder)) } what best method use?
you're saving result of test-path $pathexists-variable outside loop, before $deviceid exists. result false. variable never changes false every time inside loop. need run test-path inside loop.
also, should avoid saving , reading wmi-output file. try:
$devicedrive = "c:" get-wmiobject win32_logicaldisk -filter "drivetype=3 , deviceid!='c:'" | where-object { test-path "$($_.deviceid)\apps\netprobent\" } | foreach-object { $devicedrive = $_.deviceid }
Comments
Post a Comment