As you see in this log, the programs starts without first disk testing:
That means that it sees the drive letter but not the temp folder itself. I have removed the tests for the volume, Try this.
# Update 19-Dec - Check for the folder and driveletter, not the volume.
Start-Transcript -Path ("C:\temp\{0}-{1}-PSConsole.txt" -f $env:USERNAME, (get-date -format "yyyyMMdd-HHmmss" )) # This is your log file
$Folder = $env:TEMP # This is the target folder that we need to access. Did you use this one?
###$Folder = "B:\temp1" # This is the target folder that we need to access.
"Temp folder is {0}" -f $Folder
$Wait = $true
$count = 0
$limit = 10 # how many times should we retry (10 minutes)
$launch = $false
$DriveLetter = $Folder.Split(":")[0]
"{0} - I am going wait on the {1} drive." -f (get-date), $DriveLetter
"{0} - Here are the volumes that I see." -f (get-date)
Get-Volume | Format-Table
Try {
Get-Item "$($DriveLetter):\" -ErrorAction Stop
"{0} - I see the {1} drive!" -f (get-date), $DriveLetter
} catch {
"{0} - I don't see a {1} drive." -f (get-date), $DriveLetter
}
""
"{0} - Main loop." -f (get-date)
while ($wait) {
$Drive = Get-Item "$($DriveLetter):\" -ErrorAction SilentlyContinue
if ($Drive) { # do we see the drive letter
"{0} - I see the drive." -f (get-date)
New-Item -Path $Folder -ItemType Directory -ErrorAction SilentlyContinue # Try to create the temp folder @ 19-Dec update
if (Test-Path $Folder) {
"{0} - I see the folder. Trying to write to a file." -f (get-date)
"Test" | Out-File "$Folder\Test.txt" # verify that we can write to the folder
if (Test-Path -Path "$Folder\Test.txt") {
"{0} - I see the file!. Ok to proceed. " -f (get-date)
remove-item -Path "$Folder\Test.txt"
$launch = $true # It's ok to run the program
} Else {
"{0} - File not found. Something is wrong. " -f (get-date)
}
$wait = $false
continue
} else {
"{0} - I don't see the folder, testing the path." -f (get-date)
if (Test-Path $Folder) {
"{0} - I see the folder, but not the volume! Trying to write to a file." -f (get-date)
"Test" | Out-File "$Folder\Test.txt" # verify that we can write to the folder
if (Test-Path -Path "$Folder\Test.txt") {
"{0} - I see the file!. Ok to proceed. " -f (get-date)
remove-item -Path "$Folder\Test.txt"
$launch = $true # It's ok to run the program
} Else {
"{0} - File not found. Something is wrong. " -f (get-date)
}
$wait = $false
continue
}
}
}
$count++
if ($count -gt $limit) {
"{0} - I give up. Here are the volumes that I saw." -f (get-date)
Get-Volume | Format-Table
$Wait = $false
continue
}
"{0} - Sleeping, waiting for the {1} drive." -f (get-date), $DriveLetter
start-sleep -Seconds 60 # how long do we wait before retry
}
if ($launch) {
"{0} - Launching the program." -f (get-date)
#------------------------------------------------------------------------------
# Put your program and any arguments on the next line.
#------------------------------------------------------------------------------
whoami.exe
}
"{0} - Terminating." -f (get-date)
Stop-Transcript