如何在 Windows 10 中登录用户帐户时运行 Powershell 文件

如何在 Windows 10 中登录用户帐户时运行 Powershell 文件

我有一个 ps1 (Powershell) 文件,我想在用户登录 Windows 10 上的帐户时运行它。我尝试过其他类似问题的答案,但似乎都不起作用。这在 Windows 10 上可行吗?谢谢。

Powershell 文件:

(New-Object Media.SoundPlayer "C:\Users\Public\Public Recorded TV\Sample Media\System32.wav").PlaySync()
set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value TrollFace.jpg
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$file = (get-item 'C:\TrollFace.jpg')
#$file = (get-item "C:\TrollFace.jpg")
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume
{
    // f(), g(), ... are unused COM method slots. Define these if you care
    int f(); int g(); int h(); int i();
    int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
    int j();
    int GetMasterVolumeLevelScalar(out float pfLevel);
    int k(); int l(); int m(); int n();
    int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
    int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice
{
    int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator
{
    int f(); // Unused
    int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio
{
    static IAudioEndpointVolume Vol()
    {
        var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
        IMMDevice dev = null;
        Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
        IAudioEndpointVolume epv = null;
        var epvid = typeof(IAudioEndpointVolume).GUID;
        Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
        return epv;
    }
    public static float Volume
    {
        get { float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v; }
        set { Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty)); }
    }
    public static bool Mute
    {
        get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
        set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
    }
}
'@
while($true){
[audio]::Volume  = 1
[audio]::Mute = $false


$img = [System.Drawing.Image]::Fromfile($file);

# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $img.Size.Width;
$form.Height =  $img.Size.Height;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width;
$pictureBox.Height =  $img.Size.Height;

$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}

答案1

您可以使用 Windows 任务计划程序在用户登录时运行该文件。

指示:

  • 按 Winkey + R 打开运行窗口,然后输入 Taskschd.msc 并按回车键。
  • 在“操作”菜单(右侧)下单击“创建任务”
  • 在“常规”选项卡下,根据需要为任务命名(如果您正在执行任何需要管理员权限的操作,您可能需要选中“以最高权限运行”复选框)

  • 在“触发器”选项卡下单击“新建”

  • 在“开始任务”下选择“登录时”

  • 在“设置”下选择“任何用户”

  • 按“确定”

  • 在“操作”选项卡下选择“新建”

  • 在“操作”下选择“启动程序”

  • 在“设置”下选择脚本的位置

  • 点击“确定”

  • 在“条件”选项卡下取消选择“仅当计算机使用交流电源时才启动任务”

  • 在“设置”选项卡下,选择“在错过预定的启动后尽快运行任务”

  • 点击“确定”

相关内容