Hello Stackoverflow users,
I am noob at scripting and powershell.
I have the following script that gets all the drive letters on the host into a text file. I need to get the correct drive letter into a variable by performing a test-path. However it is not working. I know I am close but cannot get it to work. Does anyone know how to fix the script?
Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND 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 = "$_\Apps\NetprobeNT\"
$PathExists = Test-Path $Path
foreach ($DeviceID in $DeviceID)
{
If ($PathExists -eq $True)
{
$DeviceDrive = $DeviceID}
Else
{
$DeviceDrive = "C:"}
}
I think the following line is the problem
$Path = "$_\Apps\NetprobeNT\"
Any ideas on how to get this working?
This relates to PowerShell - drive variable for more information.
Thank you in advance.
$_variable is not defined anywhere. It represents the current object only within{script block}ofForEach-Object,Where-Objectetc. cmdlets. - JosefZ