Como executar um arquivo Powershell no login para uma conta de usuário no Windows 10

0

Eu tenho um arquivo ps1 (Powershell) que gostaria de executar sempre que um usuário faz login em sua conta no Windows 10, tentei outras respostas para perguntas semelhantes, mas nenhuma delas parece funcionar. Isso é possível no Windows 10 ou não? Obrigado.

Arquivo do 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()
}
    
por theman26 22.07.2017 / 23:15

1 resposta

0

Você pode usar o agendador de tarefas do Windows para executar o arquivo quando o usuário fizer login.

Instruções:

  • Pressione Winkey + R para abrir a janela de execução e digite Taskschd.msc e  aperte enter.
  • No menu "Ação" (à direita), clique em "Criar tarefa"
  • Na guia "Geral", nomeie a tarefa como quiser (você pode querer  marque a caixa de seleção "Executar com privilégios mais altos" se estiver fazendo algo que requer privilégios de administrador)

  • Na guia "Trigger" Clique em "Novo"

  • Em "Iniciar a tarefa", escolha "No logon"

  • Em Configurações, selecione "Qualquer usuário"

  • Pressione "OK"

  • Na guia "Ação", selecione "Novo"

  • Em "Ação", selecione "Iniciar um programa"

  • Escolha o local do script em "Configurações"

  • Clique em "OK"

  • Na guia "Condições" Desmarque "Iniciar a tarefa somente se o computador estiver ativado Potência CA "

  • Na guia "Configurações", selecione "Executar tarefa assim que possível após um o início agendado está ausente "

  • Clique em "Ok"

por 23.07.2017 / 00:33