Powershell – logon script based on username

Devised this script that will run a program based on the username of the logged in user and resizes the window as needed.

 

#find the currently logged on user and stick the name into the $LoggedOnUser variable
$LoggedOnUser=(Get-WmiObject -Class Win32_Process | Where-Object {$_.ProcessName -eq ‘explorer.exe’}).GetOwner() | select User
echo $LoggedOnUser

#Uncomment the line below and add into line 2 place if using on a local machine
#$LoggedOnUser=Get-WMIObject -class Win32_ComputerSystem | select username
#if statement now checking who is who.

If ($LoggedOnUser.user -eq “abc1”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc2”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc3”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc4”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc5”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc6”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc7”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc8”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc9”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc10”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc11”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc12”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc13”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc14”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc15”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc16”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc17”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abc18”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}
elseif ($LoggedOnUser.user -eq “abcz19”)
{
& “C:Windowstrace32.exe”
& “C:Windowsregedit.exe”
}

#final error trap : no username recognised ? Give them a popup box
else
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“This account has not been assigned a citrix autorun. Now logging off ”, “Citrix Presentation”, [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
}

#makes sure the programs are open by delaying the script by 15 seconds
start-sleep -s 15
#find process now and move it
Add-Type @”
using System;
using System.Runtime.InteropServices;

public class Win32 {
[DllImport(“user32.dll”)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
“@
[int]$X = 960
[int]$Y = 0
[int]$nWidth = 960
[int]$nHeight =1080
[string]$processName = “proficyclient”

$handle = (Get-Process | where {$_.ProcessName -eq $processName}).MainWindowHandle

[Win32]::MoveWindow($handle, $X, $Y, $nWidth, $nHeight, $true )

#find second process
Add-Type @”
using System;
using System.Runtime.InteropServices;

public class Win32 {
[DllImport(“user32.dll”)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
“@
[int]$X = 0
[int]$Y = 0
[int]$nWidth = 960
[int]$nHeight =1080
[string]$processName = “vlc”

$handle = (Get-Process | where {$_.ProcessName -eq $processName}).MainWindowHandle

[Win32]::MoveWindow($handle, $X, $Y, $nWidth, $nHeight, $true )

$a = new-object -comobject wscript.shell
$b = $a.popup(“Please wait 15 seconds. This window will dissapear after 15 seconds “,15,”Wait”)