none
Using Windows Popup options to run conditional Powershell code RRS feed

  • Question

  • Hi there,

    I am setting up some tasks on a schedule and want to run them based on user input. The first of these is a very basic database backup, with the option to backup now or to delay for 5 minutes, to give people the chance to finish what they are doing.

    What I am missing however, is the format to accept the pre-programmed windows button input.

    Code below:

    (new-object -ComObject wscript.shell).Popup("Do you want to start the backup now?",30,"Backup to USB",0x4)
    If ($intAnswer -eq -1) {
     Copy-Item -Path "path" -destination "path" -Force
     (new-object -ComObject wscript.shell).Popup("Backup completed",0,"Backup to USB")
    }
    If ($intAnswer -eq 6) {
     Copy-Item -Path "path" -destination "path" -Force
     (new-object -ComObject wscript.shell).Popup("Backup completed",0,"Backup to USB")
    }
    If ($intAnswer -eq 7) { 
    (new-object -ComObject wscript.shell).Popup("Backup will ask again in 5 minutes",0,"Backup to USB")
     start-sleep -s 300
     loop
     }

    The WShell option "0x4" gives the users buttons 'Yes' (condition 6 in WShell) and 'No' (condition 7 in WShell), and if the window times out (condition -1) it should back up anyway, since the file is not likely to be modified in the time it takes to copy.

    However, in the above, the conditions are being read as input text and not conditions, causing a PS window to open and input "6" rather than running the code.

    My question is: what is the format for PS to accept the WShell button condition?

    I can create and name buttons of my own to do it, but it seems clunky and introduces a POF that I would like to avoid.

    Thanks in advance

    Natasha

    Wednesday, July 13, 2016 3:49 AM

Answers

  • The first line of your code never assigns a value to $intAnswer.

    It never gets set, so none of your  if  statements will evaluate to $true.

    You need to assign a value to the variable. Like this:


    $wshShell = New-Object -ComObject "WScript.Shell"
    $answer = $wshShell.Popup("Do you want to start the backup now?", 30, "Backup to USB", 4)
    

    The "int" prefix in the variable's name is unnecessary.


    -- Bill Stewart [Bill_Stewart]


    • Edited by Bill_StewartModerator Wednesday, July 13, 2016 9:15 PM
    • Proposed as answer by jrv Wednesday, July 13, 2016 9:59 PM
    • Marked as answer by Natasha Loveday Wednesday, July 13, 2016 11:34 PM
    Wednesday, July 13, 2016 9:15 PM
    Moderator

All replies

  • intAnswer is never being set.  In fact the code is a copy of VBScript code that will not work in PowerShell.

    From the code it is not possible to understand what you are trying to do.

    $sh=new-object -ComObject wscript.shell
    if( 6 -eq $sh.Popup("Do you want to start the backup now?",30,"Backup to USB",0x4)){
    	Write-Host 'backup data'
    }else{
        Write-Host 'dont backup data'
    }
    
    
    
    


    \_(ツ)_/

    • Proposed as answer by jrv Wednesday, July 13, 2016 9:59 PM
    Wednesday, July 13, 2016 4:21 AM
  • Okay I'm very new so I'm going to ask a silly question here - in this case does yours work because you're saying 'Shell is now wscript' and then 'if shell variable = 6'? So it emulates the wscript input? Or am I interpreting that wrong?
    Wednesday, July 13, 2016 8:58 PM
  • The first line of your code never assigns a value to $intAnswer.

    It never gets set, so none of your  if  statements will evaluate to $true.

    You need to assign a value to the variable. Like this:


    $wshShell = New-Object -ComObject "WScript.Shell"
    $answer = $wshShell.Popup("Do you want to start the backup now?", 30, "Backup to USB", 4)
    

    The "int" prefix in the variable's name is unnecessary.


    -- Bill Stewart [Bill_Stewart]


    • Edited by Bill_StewartModerator Wednesday, July 13, 2016 9:15 PM
    • Proposed as answer by jrv Wednesday, July 13, 2016 9:59 PM
    • Marked as answer by Natasha Loveday Wednesday, July 13, 2016 11:34 PM
    Wednesday, July 13, 2016 9:15 PM
    Moderator