Link to home
Start Free TrialLog in
Avatar of Christophe
ChristopheFlag for France

asked on

Powershell do until

Hello,

I am trying to write script but I have no enough powershell experience to do that. I would like to have a loop until a condition is true.

For my project I need to check if a CD or dvd inserted is blank or not. I found I can check with "Get-Volume | select -property operationalStatus | Where-Object {$_.operationalStatus -eq "Unknown"} " When media is blank operationalstatus is alway Unknown.

I have started to write like that (see below) but it does not work. I am not surprised I do not know how to do that :-)


I would like to continue to show a warning until blank CD is inserted and then continue to action. I do not know how to build a loop until the condition is true.

Thank you in advance for your help and sorry for my bad english. I am a french who did not work during english lessons at school :-)

$OS = Get-Volume | select -property operationalStatus | Where-Object {$_.operationalStatus -eq "Unknown"} 

Do{

If ($OS.operationalStatus -eq  "Unknown")

{ 
 
Start-Process -Wait "C:\Program Files (x86)\softwarexxx"

 
}
Else {

[System.Windows.Forms.MessageBox]::Show("CD/DVD inserted is not blank")
Start-Process -Wait "C:\Program Files (x86)\CDBurnerXP\cdbxpcmd.exe" -ArgumentList "--eject [ -device:1 ]"
[System.Windows.Forms.MessageBox]::Show("Please insert a blank CD/DVD and click OK")
Start-Process -Wait "C:\Program Files (x86)\CDBurnerXP\cdbxpcmd.exe" -ArgumentList "--wait-for-disc [ -device:$Devicenumber ]"
}

}
Until ($OS -eq  "Unknown")

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

if((test-path -Path 'env:ProgramFiles(x86)\CDBurnerXP\cdbxpcmd.exe'))
{
  Add-Type -AssemblyName System.Windows.Forms
  $cdinstalled = $false
  do {
    [System.Windows.Forms.MessageBox]::Show("CD/DVD inserted is not blank")
    Start-Process -Wait "C:\Program Files (x86)\CDBurnerXP\cdbxpcmd.exe" -ArgumentList "--eject [ -device:1 ]"
    [System.Windows.Forms.MessageBox]::Show("Please insert a blank CD/DVD and click OK")
    Start-Process -Wait "C:\Program Files (x86)\CDBurnerXP\cdbxpcmd.exe" -ArgumentList "--wait-for-disc [ -device:$Devicenumber ]"
    $StatusLists = Get-Volume | Select-Object -ExpandProperty operationalStatus
    #$StatusLists | Format-List
    #There can be many disks returned from get-volume
    #so we have to check them all
    
    foreach($OS in $StatusLists)
      {
      if($OS -eq "Unknown")
        { 
        $cdinstalled = $true
        break  # leave loop
        }
    }
    }
  until($cdinstalled -eq $true) 

    Write-Output("Blank CD Inserted") 
    Start-Process -Wait "C:\Program Files (x86)\softwarexxx"

}
Else {
    write-output ("CDBurnerXP is not installed")
    }

Open in new window

Where are you going to define $Devicenumber in line 9
SOLUTION
Avatar of Christophe
Christophe
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Christophe

ASKER

Hello,

I have just tried your script. I am sorry it does not work like it should works. I have inserted a blank CD and I get message "CD/DVD inserted is not blank".

First process I have inserted blank CD. I've got "CD/DVD inserted is not blank". Script continues to steps "eject, ("Please insert a blank CD/DVD and click OK"), wait-for-disc. I just inserted same blank CD and script has continued to step   Write-Output("Blank CD Inserted")

Second process I've tried with non blank CD. I've got "CD/DVD inserted is not blank". Script continues to steps "eject, ("Please insert a blank CD/DVD and click OK"), wait-for-disc. I've inserted blank CD and script has continued to step   Write-Output("Blank CD Inserted"). it works

It is just for the first process that there is a problem. If a blank CD is already inserted script should continue to step   Write-Output("Blank CD Inserted") and not to "CD/DVD inserted is not blank" step

Thanks
Avatar of oBdA
oBdA

Whose "your" script?
David Johnson. I did not tried your script yet (oBdA)

Thanks for your help
Awesome! Very good script. It works perfectly. Thank you for your help!