Link to home
Create AccountLog in
Avatar of mrc032
mrc032

asked on

Schut down Virtual Machines if not used for * time tru powershell

Can someone help me with a powershell script what will do:
Schut down Virtual Machines if not used for * time tru powershell

(Situation with multiple Hyper-V clusters)
Avatar of Navdeep
Navdeep
Flag of Singapore image

Hi,

What is the time limit? I mean like you want if the system is not used x no. of days/hours ?

Also i assume that you are trying to say that if system is up and running from x no. of days/hours then shut it down

When you say use. Then it will not be possible to do what you are trying becauseAs long as system is running it means it's being in use
Avatar of mrc032
mrc032

ASKER

Hmm, good question. Maybe the question should be: X days of not being logged on. (x=7 days)
Hi,

will you be running the script from remote machine or on the machine itself?
Avatar of mrc032

ASKER

Remote would be the best sollution for us.
ASKER CERTIFIED SOLUTION
Avatar of Navdeep
Navdeep
Flag of Singapore image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mrc032

ASKER

Thanks, gonna check ik out today!
Avatar of mrc032

ASKER

Comment to that. This script wil schut down a vm if vm is up and running for 1 hour. This is then regardless of someone is using the vm?
Avatar of mrc032

ASKER

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Tooling\Shutdown_after_X_days\script.ps1:5 char:21
+ $wmi = get-wmiobject <<<<  -class win32_OperatingSystem -computer $ComputerName
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_X_days\script.ps1:6 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
    + CategoryInfo          : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'.
At C:\Tooling\Shutdown_after_X_days\script.ps1:7 char:33
+ [Timespan]$uptime = New-TimeSpan <<<<  $LastRebootTime $(get-date)
    + CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewTimeSpanCommand
Hi seems like you are not able to connect to remote system using WMI or WMI is not responding properly.

can you run the script for just one target computer to find out if it's working correctly. Before that run the following command to check wmi functionality at command prompt

wmic share

Avatar of mrc032

ASKER

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Tooling\Shutdown_after_X_days\script.ps1:5 char:21
+ $wmi = get-wmiobject <<<<  -class win32_OperatingSystem -computer $ComputerName
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_X_days\script.ps1:6 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
    + CategoryInfo          : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'.
At C:\Tooling\Shutdown_after_X_days\script.ps1:7 char:33
+ [Timespan]$uptime = New-TimeSpan <<<<  $LastRebootTime $(get-date)
    + CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewTimeSpanCommand

Still have Sometime

(WMIC Share on target or * computer? Did the test on other one where script is)
hi,

CompterA------ComputerB

are you trying to shut down B from A

----
Run this command on each computer and let me know if you see any output

get-wmiobject -class win32_OperatingSystem -computer ComputerA
get-wmiobject -class win32_OperatingSystem -computer ComputerB

-------

If it works now from Computer A issue the command and let me know if it still works

get-wmiobject -class win32_OperatingSystem -computer ComputerB
Avatar of mrc032

ASKER

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 7600
RegisteredUser  : Windows User
SerialNumber    : 00486-001-0001076-84676
Version         : 6.1.7600


SystemDirectory : C:\WINDOWS\system32
Organization    : ***
BuildNumber     : 2600
RegisteredUser  : ****
SerialNumber    : 76487-642-*******-****
Version         : 5.1.2600

Stars are hidden ;-)
how abt this

If it works now from Computer A for ComputerB iissue the command and let me know if it still works

get-wmiobject -class win32_OperatingSystem -computer ComputerB
Avatar of mrc032

ASKER

Yes this works. I did that already the first time, forgot to mension that. Sorry.
So script should work.

if you check the error it says Get-WmiObject : The RPC server is unavailable.

this means some network or wmi issue. WMI is not able to talk to other computer.

anyways need to test the code line by line on your env. On my side it works fine.
-----------------------------------------------------------
Run this code line by line
Copy each line, paste it in PowerShell console and let me know if you get any error
-----------------------------------------------------------

$wmi = get-wmiobject -class win32_OperatingSystem -computer RemoteComputer
$LastRebootTime = $wmi.ConvertToDateTime($wmi.LastBootupTime)
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours
------------------------------------------------------------------

Avatar of mrc032

ASKER

PS C:\Users\a-> $LastRebootTime = $wmi.ConvertToDateTime($wmi.LastBootupTime)
You cannot call a method on a null-valued expression.
At line:1 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
    + CategoryInfo          : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\Users\> $LastRebootTime = $wmi.ConvertToDateTime($wmi.LastBootupTime)
You cannot call a method on a null-valued expression.
At line:1 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
    + CategoryInfo          : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\Users\> [Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'.
At line:1 char:33
+ [Timespan]$uptime = New-TimeSpan <<<<  $LastRebootTime $(get-date)
    + CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewTimeSpanCommand

PS C:\Users> $uptime.hours
This is the output from my laptop

PS C:\Users\user> $wmi = get-wmiobject -class win32_OperatingSystem -computer user-pc
$LastRebootTime = $wmi.ConvertToDateTime($wmi.LastBootupTime)
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours

1

It works fine here. May be something issue is something else... not the script.
Avatar of mrc032

ASKER

I can't get it to work just yet but it should be working.
Avatar of mrc032

ASKER

V-nas Thanks for the help. I gonna figur it out why it isn;'t working here.