Restarting Script

Hello all,

I have a new script I’m working on, and I have it working pretty good until the end. I have a menu that allows the user to run a task, and after it runs it, I want to ask the user if they want to run another task or not. If they don’t, I just want to exit. If they do however, I want to return to where it asks what schedule they want to run.

$caption = "Choose SCCM Schedule";
Write-Host "SCCM Task Scheduler" -Fore Magenta
Write-Host
$machine = Read-Host 'What is the machine name?'
Write-Host
$SMSCli = [wmiclass] "\\$machine\root\ccm:SMS_Client"
[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ){
Write-host "1. Hardware Inventory" -fore Cyan
Write-host "2. Software Inventory" -fore Cyan
Write-host "3. Discovery Data" -fore Cyan
Write-Host
[Int]$xMenuChoiceA = read-host "Select Task to Schedule"}
Switch( $xMenuChoiceA ){
  1{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000001}"), "Scheduling Hardware Inventory"; break}
  2{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000002}"), "Scheduling Software Inventory"; break}
  3{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000003}"), "Scheduling Discovery Data"; break}
}
Write-Host
$message = Write-Host "Do you want to schedule another task?" -foregroundcolor Red
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes" 
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0) 
switch ($result)
{
        0 {"You Chose Yes"}
        1 {"You Chose No"}
}

Basically, if they pick yes, I want it tor return to the block that has

[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ){
Write-host "1. Hardware Inventory" -fore Cyan
Write-host "2. Software Inventory" -fore Cyan
Write-host "3. Discovery Data" -fore Cyan
Write-Host
[Int]$xMenuChoiceA = read-host "Select Task to Schedule"}

just made a small adjustment :)…see if this works for you…

$caption = "Choose SCCM Schedule"; Write-Host "SCCM Task Scheduler" -Fore Magenta Write-Host $machine = Read-Host 'What is the machine name?' Write-Host $SMSCli = [wmiclass] "\\$machine\root\ccm:SMS_Client" [int]$xMenuChoiceA = 0 while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ) { Write-host "1. Hardware Inventory" -fore Cyan Write-host "2. Software Inventory" -fore Cyan Write-host "3. Discovery Data" -fore Cyan Write-Host [Int]$xMenuChoiceA = read-host "Select Task to Schedule"

Switch( $xMenuChoiceA )
{
1{“Scheduling Hardware Inventory”; break}
2{“Scheduling Software Inventory”; break}
3{“Scheduling Discovery Data”; break}
}
Write-Host
$message = Write-Host “Do you want to schedule another task?” -foregroundcolor Red
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {$xMenuChoiceA = 0}
1 {“You Chose No”}
}

}

This worked perfectly, thank you!

So another question on this type of scenario…In the same code, I’ve added a ping feature to the top that will ping the machine name before proceeding. Now, I have it setup so that it pings, and if it finds the machine it will continue to the next menu option. My hiccup is trying to get it to prompt for the user to re-enter the machine name, and then re-run the whole pinging section on the new machine name. I can make it pop-up with a prompt for a new machine name, however the results are very strange.

If the re-entered machine name is valid, it doesn’t ping and just continues to the next part of the script.

If it is invalid again, it acts like it is pinging again, but I get an error:

“Cannot convert value “\PTLW95S7YY1\root\ccm:SMS_Client” to"System.Management.ManagementClass”…

So it looks like it’s proceeding with the code as normal here but it can’t find the machine, so instead of pinging and re-prompting to fix it just continues.

I tried messing around with what you did with the last situation where I was trying to re-run part of the script, but I can’t seem to get anything to work in full.

$caption = "Choose SCCM Schedule";
Write-Host "`nSCCM Task Scheduler" -Fore Magenta
Write-Host
$machine = Read-Host 'What is the machine name?'
Write-Host
Write-Host "Pinging Machine..." -Fore Yellow
Write-Host
[b]if [Test-Connection $machine -Quiet]{
Write-Host "Machine Found!" -Fore DarkGreen
}
else {
Write-Host "Machine Not Found - Check Name" -Fore Red
$machine = Read-Host 'What is the machine name'
}
[/b]Write-Host
$SMSCli = [wmiclass] "\\$machine\root\ccm:SMS_Client"
[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 )
{
Write-host "1. Hardware Inventory" -fore Cyan
Write-host "2. Software Inventory" -fore Cyan
Write-host "3. Discovery Data" -fore Cyan
Write-Host 
[Int]$xMenuChoiceA = read-host "Select Task to Schedule"

Switch( $xMenuChoiceA )
{
  1{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000001}"), "Scheduling Hardware Inventory"; break}
  2{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000002}"), "Scheduling Software Inventory"; break}
  3{$SMSCli.TriggerSchedule("{00000000-0000-0000-0000-000000000003}"), "Scheduling Discovery Data"; break}
}
Write-Host
$message = Write-Host "Do you want to schedule another task?" -foregroundcolor Green
Write-Host
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
        0 {$xMenuChoiceA = 0}
        1 {"You Chose No"}
}

}

I tried doing it with a IF statement on the TRUE/FALSE output of the Test-Connection command, but no luck:

#-----Start Ping Target Machine-----#
$Pingcheck = {
Write-Host "Pinging Machine..." -Fore Yellow
$Ping = Test-Connection $machine -Quiet
if ($Ping -eq $true){
Write-Host "Machine Found!" -Fore DarkGreen
}
else{
if ($Ping -eq $false){
Write-Host "Machine Not Found - Check Name" -Fore Red
$machine = Read-Host 'What is the machine name'
&$Pingcheck
}
}
}

#-----End Ping Target Machine-----#

let’s consider this piece of code:

if (Test-Connection $machine -Quiet) { Write-Host "Machine Found!" -Fore DarkGreen } else { Write-Host "Machine Not Found – Check Name" -Fore Red $machine = Read-Host 'What is the machine name' }

If I enter a machine name say “doesnotexist” the test-connection in the IF block runs, finds that the machine does not exist and returns a false so the script execution goes to the else block.
In the else block the read-host runs again and because there isnt a second ping check within the else block the $machine variable gets whatever name you put in, valid or invalid and executes the next set of commands.
In order to correct this you would need a loop that repeats the “Ping” portion of the script until a valid machine name is entered.

You can try the code below or modify to fit your needs:

Note: Connection to a machine may succeed on ping but fail on wmi due to domain\workgroup scenario etc…so you may consider adding a wmi check immediately following the ping check.

$caption = "Choose SCCM Schedule"; Write-Host "SCCM Task Scheduler" -Fore Magenta Write-Host do { $machine = Read-Host 'What is the machine name?' Write-Host Write-Host "Pinging Computer $machine......`n" $ping = Test-Connection -ComputerName $machine -Count 2 -Quiet
if($ping)
{
    "Ping Succeeded`n"
    $SMSCli = [wmiclass] "\\$machine\root\ccm:SMS_Client"

    [int]$xMenuChoiceA = 0
    while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 )
    {
        Write-host "1. Hardware Inventory" -fore Cyan
        Write-host "2. Software Inventory" -fore Cyan
        Write-host "3. Discovery Data" -fore Cyan
        Write-Host
        [Int]$xMenuChoiceA = read-host "Select Task to Schedule"

        if($xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 3 ) { "Invalid Choice`n";Continue} 

        Switch( $xMenuChoiceA )
        {
          1{"Scheduling Hardware Inventory"; break}
          2{"Scheduling Software Inventory"; break}
          3{"Scheduling Discovery Data"; break}
        }

        Write-Host
        $message = Write-Host "Do you want to schedule another task?" -foregroundcolor Red
        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes" 
        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
        $Mainmenu = New-Object System.Management.Automation.Host.ChoiceDescription "Mainmenu"
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no,$Mainmenu)
        $result = $host.ui.PromptForChoice($title, $message, $options, 0) 

        switch ($result)
        {
                0 {$xMenuChoiceA = 0}
                1 {"You Chose No";$ping=$true}
                2 {$ping=$false}
        }

    }#while xmenu
}
Else
{
    "ping failed"
    
}

}
While(-not $ping)