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

Home Forums Scripting PowerShell Trying to get A batrch file to run from a PS script that displays to the screen

This topic contains 4 replies, has 5 voices, and was last updated by Avatar weirdo 2 years, 10 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Avatar
    jason0923
    Participant
    #166840

    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 datashutdown.Csv”
    $check = (Import-Csv $csv).”Computername”
    $check -contains “$hostname”
    $check2 = $check -contains “$hostname”

    If($check2 -eq$true){start-process ‘c:user dataask.cmd’
    }

    Batch File:

    [USER=”68995″]Echo[/USER] 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
    )

    Avatar
    Anonymous
    #372013

    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/Forums/scriptcenter/en-US/57550bdc-550d-49a2-9551-ad739b27263b/powershell-yesno-popup?forum=ITCG

    VB does this sort of thing natively, and since PS can access .NET assemblies, the sky’s the limit.

    Avatar
    weirdo
    Member
    #390570

    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?

    Avatar
    Ossian
    Moderator
    #191541

    I don’t know if it helps, but the old AT command had a /interactive switch (https://support.microsoft.com/en-us/help/313565/how-to-use-the-at-command-to-schedule-tasks)
    WIll try to see if there is a powershell equivalent, but don’t recall ever seeing such an option

    Rems
    Rems
    Moderator
    #228475
    jason0923;n509183 wrote:
    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 – [URL=”https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx”]https://msdn.microsoft.com/en-us/lib…(v=vs.84).aspx[/URL]
    $wshell = New-Object -ComObject Wscript.Shell
    $confirmation = $wshell.Popup(“Your computer is scheduled to shutdown within 10 min.nnClick 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′
    }

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.