Shutdown powershell script

Soldato
Joined
1 Dec 2006
Posts
16,834
Location
Amsterdam, NL
Ahhhh! In over my head a little.

I've pulled this from the net, customised it a little. But regardless what I do it will just run the shutdown command even if I'm logged in.

It SHOULD see that I'm logged in and then run the vbs script which gives me a prompt to postpone.

Here is the script:

Code:
$comp = "."
$Loggedon = Get-WmiObject -Class win32_computersystem -ComputerName $comp | select username
$Resultsof = $Loggedon -like '*DOMAIN-NAME*'

IF ($Resultsof -like "False")
{
$eventSource = "AfterHours Shutdown Policy"
if (![Diagnostics.EventLog]::SourceExists($eventSource))
{
[Diagnostics.EventLog]::CreateEventSource($eventSource, "Application")
}

[Diagnostics.EventLog]::WriteEntry($eventSource, "No Users Logged On. Computer left running after hours - forced shutdown", [Diagnostics.EventLogEntryType]::Information)
shutdown.exe -s -t 0 -c "Computer left running after hours - forced shutdown"}

ElseIF ($Resultsof -like "True")

{
$eventSource = "AfterHours Shutdown Policy"
if (![Diagnostics.EventLog]::SourceExists($eventSource))
{
[Diagnostics.EventLog]::CreateEventSource($eventSource, "Application")
}

[Diagnostics.EventLog]::WriteEntry($eventSource, "$Loggedon is logged on. Computer left running after hours - forced shutdown", [Diagnostics.EventLogEntryType]::Information)
\\COMPUTER NAME\C$\Shutdown\psexec.exe /accepteula \\localhost -u local-user>n -p password> -i 1 wscript.exe \\COMPUTER NAME\C$\Shutdown\RemoteShutdown.vbs}

Can anyone help? I just want it to check if a user is logged in, if no, then shutdown, if so, then run the vbs script.

Thanks
 
Back
Top Bottom