【问题标题】:How to call WPF app in powershell after windows 7 logoffwindows 7注销后如何在powershell中调用WPF应用程序
【发布时间】:2014-05-22 14:44:06
【问题描述】:

Windows 7 注销后我不会调用我的 wpf 应用程序。我编写了一个调用我的 wpf 应用程序并等待 wpf 应用程序关闭的 powershell 脚本。 代码

$p = New-Object System.Diagnostics.Process
$pinfo = New-Object System.Diagnostics.ProcessStartInfo("D:\PowerShell\PsWpf.exe");
$p.StartInfo = $pinfo;
$p.Start();
$p.WaitForExit();  

当我尝试将此脚本用作注销脚本时,在 Windows 设置-->脚本-->注销下的 gpedit.msc 中配置,然后我的计算机冻结,这意味着没有反应。

如果我像往常一样执行脚本,这意味着右键单击 powershell 文件,然后“使用 Powershell 运行”,一切运行正常。

Windows 注销后如何在 powershell 中调用我的 wpf 应用程序?

我知道可以在 powershell 中使用 wpf,比如

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
[xml]$xaml = 
@"
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PowerShell WPF" Height="244" Width="525" FontFamily="Calibri" FontWeight="Bold" ResizeMode="NoResize">
    <Grid Background="#58000000">
        <Label Content="Designed and Developed by Free Lancer" Height="28" HorizontalAlignment="Left" Margin="288,165,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>
</Window>

"@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )


$Window.ShowDialog() | Out-Null

并将其用作注销脚本,它可以正常工作。我的问题是,我不能仅在 .NET 中使用 powershell 编写脚本。

【问题讨论】:

    标签: c# wpf windows powershell


    【解决方案1】:

    根据我从您当前形式的问题中提取的内容(说实话有点令人困惑),我认为您想要:

    1. 在注销事件时启动进程
    2. 使用组策略这样做
    3. 暂停注销,直到脚本操作和进程完成

    我在摸索你为什么要使用:

    $p = New-Object System.Diagnostics.Process
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo("D:\PowerShell\PsWpf.exe");
    $p.StartInfo = $pinfo;
    $p.Start();
    $p.WaitForExit();  
    

    我想知道这是否会更好:

    Start-Process -FilePath "D:\Powershell\PsWpf.exe" -Wait
    

    Start-Process-Wait 参数一起使用将暂停脚本执行,直到进程“PsWpf.exe”结束。此外,您可能不知道,GPO 使用 CMD shell 而不是 Powershell 执行。因此,要从 CMD 环境执行 powershell 命令,您需要像这样封装它们:

    powershell.exe -Command "& {Start-Process -FilePath "D:\Powershell\PsWpf.exe" -Wait}
    

    如果您的计算机在关机/注销时仍然挂起,那是因为程序 PsWpf.exe 没有退出。

    【讨论】:

    • 我试试这个 Start-Process -FilePath "D:\Powershell\PsWpf.exe" -Wait -WindowStyle Normal 和 wpf 窗口显示,但它不会等到我完成程序。 wpf 窗口仅在计算机关闭后出现一秒钟。但我想要的是,计算机不会关闭,直到我完成 wpf 程序。
    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 2011-11-07
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多