【问题标题】:Run PowerShell [System.windows.forms.messagebox] through a Batch file without cmd window showing通过批处理文件运行 PowerShell [System.windows.forms.messagebox] 而不显示 cmd 窗口
【发布时间】:2020-05-22 18:31:06
【问题描述】:

我正在尝试创建一个自动化流程,该流程将在每次登录笔记本电脑时清理用户数据。主要部件已经完成,我正处于抛光阶段。我有两个批处理文件在启动时运行,它们从正在登录的配置文件中清除用户数据,它们都可以工作。我的问题与出现在我的对话框后面的 cmd 窗口有关(见图)。

研究如何让这个 cmd 框变得不可见,将我带到了 VBS。我确实找到了解决方案,但现在对话框根本不显示。我相信这可能是因为 VBS 脚本使所有窗口提示不可见。

底线是,如何在没有打开 CMD 窗口的情况下显示我的对话框?

VBScript 当前在此处的常用启动文件夹下运行:

C:\ProgramData\Microsoft\Windows\开始菜单\程序\启动

删除脚本:

@echo off

::Set color of script
color 0a

::Title
title Loaner data wipe
set userpreserve="Administrator,All Users,Default,Public,barfiej"

::All files and folders within the parent folders below will be deleted.
c:
del /S /F/ Q "C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook\*"
del /S /F/ Q "C:\Users\%USERNAME%\Contacts\*"
del /S /F/ Q "C:\Users\%USERNAME%\Desktop\*"
del /S /F/ Q "C:\Users\%USERNAME%\Documents\*"
del /S /F/ Q "C:\Users\%USERNAME%\Downloads\*"
del /S /F/ Q "C:\Users\%USERNAME%\Favorites\*"
del /S /F/ Q "C:\Users\%USERNAME%\Links\*"
del /S /F/ Q "C:\Users\%USERNAME%\Music\*"
del /S /F/ Q "C:\Users\%USERNAME%\OneDrive\*"
del /S /F/ Q "C:\Users\%USERNAME%\OneDrive - Six Continents Hotels, Inc\*"
del /S /F/ Q "C:\Users\%USERNAME%\Pictures\*"
del /S /F/ Q "C:\Users\%USERNAME%\Saved Games\*"
del /S /F/ Q "C:\Users\%USERNAME%\Searches\*"
del /S /F/ Q "C:\Users\%USERNAME%\Videos\*"

::Clear credential manager
For /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H

对话框cmd:

powershell -Command "Add-Type -AssemblyName System.Windows.Forms; C:\ProgramData\LoanerBatchFile\dialogue_box.ps1;"

dialogue_box.ps1:

Add-Type -AssemblyName System.Windows.Forms

[System.windows.forms.messagebox]::show("Welcome to your loaner computer.

`nPlease keep the follow the following instructions while using the loaner laptop.

`n- Save all documents to OneDrive. Data is set to be removed from the user profile at each logoff

`n- Use Webmail

`n- Please keep the computer clean

`n- Be sure to return loaner when picking up your computer");

VBS 脚本:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\ProgramData\LoanerBatchFile\UserDataDeletion.bat" & Chr(34), 0
WshShell.Run chr(34) & objShell.Run("C:\ProgramData\LoanerBatchFile\dialogue.bat") & Chr(34), 0
Set WshShell = Nothing

【问题讨论】:

  • 当你所做的一切都可以在 PowerShell 中完成时,为什么还要调用外部的东西?最后,您正在使用 PowerShell 调用 2 个外部可执行文件,显式 cmd.exe 和隐式 cscript.exe。因此,应该显示 cmd.exe,因为您隐式调用 cscript.exe 来运行 VBS 内容和 UI 内容,例如设置窗口标题等。如果您不想看到它,为什么要这样做?设置登录计划任务以运行您的脚本。
  • 为什么不把你的Dialog box cmd改成这个:@"%__AppData__%WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy RemoteSigned -WindowStyle Hidden -File "C:\ProgramData\LoanerBatchFile\dialogue_box.ps1"

标签: windows powershell batch-file cmd vbscript


【解决方案1】:

你可以试试另一种方法:

$wsh = New-Object -ComObject Wscript.Shell 
[Void]$wsh.PopUp("Message content here")

【讨论】:

    【解决方案2】:

    作为我上面最初评论的后续行动。

    未经测试,因为我没有什么可以测试这个概念,但是,大致类似于......

    Add-Type -AssemblyName System.Windows.Forms
    
    & cmd.exe set userpreserve="Administrator,All Users,Default,Public,barfiej"
    
    # All files and folders within the parent folders below will be deleted.
    
    'C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook\*',
    'C:\Users\%USERNAME%\Contacts\*',
    'C:\Users\%USERNAME%\Desktop\*',
    'C:\Users\%USERNAME%\Documents\*',
    'C:\Users\%USERNAME%\Downloads\*',
    'C:\Users\%USERNAME%\Favorites\*',
    'C:\Users\%USERNAME%\Links\*',
    'C:\Users\%USERNAME%\Music\*',
    'C:\Users\%USERNAME%\OneDrive\*',
    'C:\Users\%USERNAME%\OneDrive - Six Continents Hotels, Inc\*',
    'C:\Users\%USERNAME%\Pictures\*',
    'C:\Users\%USERNAME%\Saved Games\*',
    'C:\Users\%USERNAME%\Searches\*',
    'C:\Users\%USERNAME%\Videos\*' | 
    ForEach { Remove-Item -Path $PSItem -Recurse -Force}
    
    <#
    https://docs.microsoft.com/en-us/archive/blogs/rmilne/script-to-clear-credman
    #>
    
    & cmd.exe For /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H
    
    [System.windows.forms.messagebox]::show(
    "Welcome to your loaner computer.
    `nPlease keep the follow the following instructions while using the loaner laptop.
    `n- Save all documents to OneDrive. Data is set to be removed from the user profile at each logoff
    `n- Use Webmail
    `n- Please keep the computer clean
    `n- Be sure to return loaner when picking up your computer"
    )
    

    再次,将其放入分配给 RunOnce 的登录/启动计划任务中,或在登录时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2014-06-23
      • 2012-06-19
      • 1970-01-01
      • 2014-10-13
      相关资源
      最近更新 更多