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

ChristopheIT CoordinatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
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

0
David Johnson, CD, MVPOwnerCommented:
Where are you going to define $Devicenumber in line 9
0
ChristopheIT CoordinatorAuthor Commented:
Hello,

Thank you for your help. I've posted only a part of my script. I've defined $Devicenumber at other part.

I will try what you've posted and let you know.

Thanks again for your help
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

oBdACommented:
This will get a list of available writer devices, stop with an error of none found, query for the device to use if more than one is found, and use the single one found directly otherwise.
Add-Type -AssemblyName 'System.Windows.Forms'
$cdbxpcmd = "C:\Program Files (x86)\CDBurnerXP\cdbxpcmd.exe"
$Output = & $cdbxpcmd --list-drives
If ([string]::IsNullOrEmpty($Output)) {
	[void][System.Windows.Forms.MessageBox]::Show("ERROR: no supported writer found!")
	Exit 1
}
$DriveList = @($Output | Where-Object {$_ -match '\A(?<Device>\d+):(?<Name>.*)\((?<Drive>[A-Z]:)\\\)\Z'} | ForEach-Object {
	'' | Select-Object -Property @{n="Device"; e={[int]($Matches['Device'])}}, @{n='Drive'; e={$Matches['Drive']}}, @{n='Name'; e={$Matches['Name'].Trim() -replace '\s+', ' '}}
})
If ($DriveList.Count -eq 1) {
	$Writer = $DriveList[0]
} Else {
	$Writer = $DriveList | Out-GridView -Title 'Please select the Writer to use' -OutputMode Single
	If (-not $Writer) {
		[void][System.Windows.Forms.MessageBox]::Show("ERROR: Canceled by user, no writer selected!")
		Exit 1
	}
}
Write-Host "Using drive $($Writer.Drive) ($($Writer.Name)), device ID $($Writer.Device)."
Do {
	$Volume = Get-Volume -DriveLetter $Writer.Drive.Trim(':')
	If ($Volume.OperationalStatus -ne 'Unknown') {
		& $cdbxpcmd --eject -device:$($Writer.Device)
		$DialogResult = [System.Windows.Forms.MessageBox]::Show("CD/DVD inserted is not blank;`r`nPlease insert a blank CD/DVD and click OK.", 'Please insert a blank CD/DVD', ([System.Windows.Forms.MessageBoxButtons]::OKCancel))
		If ($DialogResult.ToString() -eq 'Cancel') {
			[void][System.Windows.Forms.MessageBox]::Show("ERROR: Canceled by user, no disk inserted!")
			Exit 1
		}
		Write-Host "Waiting for an empty disk to be inserted (cancel with Ctrl-C) ..." -ForegroundColor Yellow
		& $cdbxpcmd --wait-for-disc -device:$($Writer.Device)
	}
} Until ($Volume.OperationalStatus -eq 'Unknown')
Write-Host "Found an empty disk in drive $($Writer.Drive)" -ForegroundColor Green

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ChristopheIT CoordinatorAuthor Commented:
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
0
oBdACommented:
Whose "your" script?
0
ChristopheIT CoordinatorAuthor Commented:
David Johnson. I did not tried your script yet (oBdA)

Thanks for your help
0
ChristopheIT CoordinatorAuthor Commented:
Awesome! Very good script. It works perfectly. Thank you for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.