Announcement

Collapse
No announcement yet.

Trying to get A batrch file to run from a PS script that displays to the screen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to get A batrch file to run from a PS script that displays to the screen

    I have a powershell script to query a CSV and then if ... run a batch file. When I run the Script manually it does what I need and displays the batch file on the screen for the user to see. I'm trying to get it to run as a scheduled task and the task runs but the batch file (which requires user interaction) doesn't display. Is there some command in either the PS or Batch I need when run as a scheduled task?

    PS Script:

    set-executionpolicy unrestricted
    $hostname = hostname
    $csv = "c:\user data\shutdown.Csv"
    $check = (Import-Csv $csv)."Computername"
    $check -contains "$hostname"
    $check2 = $check -contains "$hostname"

    If($check2 -eq$true){start-process 'c:\user data\ask.cmd'
    }


    Batch File:

    Echo OFF
    :myLabel
    Color 0E
    SHUTDOWN /S /F /T 600
    SET /P continue="Your computer is about to shutdown in 10 min do you want to abort (y/n): "
    IF %continue% EQU y (
    SHUTDOWN /A
    )

  • #2
    Don't know about making the cmd prompt interactive, but wouldn't it be simpler to use the Powershell script to 'write-host' and put your msg in there? You can call a specific type of message window with Yes/No buttons and program the text & colors any way you'd like. This is obviously a continuation of a previous post by you for PS assistance for the beginning part of your script, and the shutdown command option in PS would be 'stop-computer'. Display the window with the message, but put that inside a 10-minute loop. If the user doesn't acknowledge by the end of the loop or types 'n', the PC shuts down. If the user does answer 'y' to the abort question, it aborts the count and quits. While PS doesn't do GUI operations as well as background admin tasks, it can be scripted to call a Yes/No dialog box, see this article I found:

    https://social.technet.microsoft.com...pup?forum=ITCG

    VB does this sort of thing natively, and since PS can access .NET assemblies, the sky's the limit.
    *RicklesP*
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **

    Comment


    • #3
      I was looking at that today but aren;t I going to run into the same problem? If the PS script is run from a Sheduled task run as the system account would the user even see the PS MSgBox?

      Comment


      • #4
        I don't know if it helps, but the old AT command had a /interactive switch (https://support.microsoft.com/en-us/...schedule-tasks)
        WIll try to see if there is a powershell equivalent, but don't recall ever seeing such an option
        Tom Jones
        MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
        PhD, MSc, FIAP, MIITT
        IT Trainer / Consultant
        Ossian Ltd
        Scotland

        ** Remember to give credit where credit is due and leave reputation points where appropriate **

        Comment


        • #5
          Originally posted by jason0923 View Post
          I was looking at that today but aren;t I going to run into the same problem? If the PS script is run from a Sheduled task run as the system account would the user even see the PS MSgBox?
          Yup the dialog box will also popup from a scheduled task.

          Scheduled Task > actions > start a program: powershell.exe -executionpolicy ByPass -file "D:\shutdown computer.ps1" -WindowStyle hidden
          Code:
          Start-Process -WindowStyle hidden -FilePath 'SHUTDOWN.exe' -ArgumentList '/A'
          
          # Display Pop-Up Window - https://msdn.microsoft.com/en-us/lib...(v=vs.84).aspx
          $wshell = New-Object -ComObject Wscript.Shell
          $confirmation = $wshell.Popup("Your computer is scheduled to shutdown within 10 min.`n`nClick Cancel to abort the shutdown",600,"Attention...", 0x1 + 0x40 + 0x1000)
          
             if ($confirmation -eq -1) {
               Start-Process -WindowStyle hidden -FilePath 'SHUTDOWN.exe' -ArgumentList '/S','/F','/T 10'
             }
             if ($confirmation -ne 2) {
               Start-Process -WindowStyle hidden -FilePath 'SHUTDOWN.exe' -ArgumentList '/S','/F','/T 600'
             }
          Last edited by Rems; 9th February 2017, 01:28.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment

          Working...
          X