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)
Schut down Virtual Machines if not used for * time tru powershell
(Situation with multiple Hyper-V clusters)
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?
will you be running the script from remote machine or on the machine itself?
ASKER
Remote would be the best sollution for us.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks, gonna check ik out today!
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?
ASKER
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Tooling\Shutdown_after_ X_days\scr ipt.ps1:5 char:21
+ $wmi = get-wmiobject <<<< -class win32_OperatingSystem -computer $ComputerName
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microso ft.PowerSh ell.Comman ds.GetWmiO bjectComma nd
You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_ X_days\scr ipt.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\scr ipt.ps1:7 char:33
+ [Timespan]$uptime = New-TimeSpan <<<< $LastRebootTime $(get-date)
+ CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun d,Microsof t.PowerShe ll.Command s.NewTimeS panCommand
At C:\Tooling\Shutdown_after_
+ $wmi = get-wmiobject <<<< -class win32_OperatingSystem -computer $ComputerName
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microso
You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
+ CategoryInfo : InvalidOperation: (ConvertToDateTime:String)
+ FullyQualifiedErrorId : InvokeMethodOnNull
New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'.
At C:\Tooling\Shutdown_after_
+ [Timespan]$uptime = New-TimeSpan <<<< $LastRebootTime $(get-date)
+ CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun
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
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
ASKER
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Tooling\Shutdown_after_ X_days\scr ipt.ps1:5 char:21
+ $wmi = get-wmiobject <<<< -class win32_OperatingSystem -computer $ComputerName
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microso ft.PowerSh ell.Comman ds.GetWmiO bjectComma nd
You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_ X_days\scr ipt.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\scr ipt.ps1:7 char:33
+ [Timespan]$uptime = New-TimeSpan <<<< $LastRebootTime $(get-date)
+ CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun d,Microsof t.PowerShe ll.Command s.NewTimeS panCommand
Still have Sometime
(WMIC Share on target or * computer? Did the test on other one where script is)
At C:\Tooling\Shutdown_after_
+ $wmi = get-wmiobject <<<< -class win32_OperatingSystem -computer $ComputerName
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microso
You cannot call a method on a null-valued expression.
At C:\Tooling\Shutdown_after_
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
+ CategoryInfo : InvalidOperation: (ConvertToDateTime:String)
+ FullyQualifiedErrorId : InvokeMethodOnNull
New-TimeSpan : A positional parameter cannot be found that accepts argument '$null'.
At C:\Tooling\Shutdown_after_
+ [Timespan]$uptime = New-TimeSpan <<<< $LastRebootTime $(get-date)
+ CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFoun
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
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
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 ;-)
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
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
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($wm i.LastBoot upTime)
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours
-------------------------- ---------- ---------- ---------- ----------
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($wm
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours
--------------------------
ASKER
PS C:\Users\a-> $LastRebootTime = $wmi.ConvertToDateTime($wm i.LastBoot upTime)
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($wm i.LastBoot upTime)
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 : PositionalParameterNotFoun d,Microsof t.PowerShe ll.Command s.NewTimeS panCommand
PS C:\Users> $uptime.hours
You cannot call a method on a null-valued expression.
At line:1 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
+ CategoryInfo : InvalidOperation: (ConvertToDateTime:String)
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS C:\Users\> $LastRebootTime = $wmi.ConvertToDateTime($wm
You cannot call a method on a null-valued expression.
At line:1 char:41
+ $LastRebootTime = $wmi.ConvertToDateTime <<<< ($wmi.LastBootupTime)
+ CategoryInfo : InvalidOperation: (ConvertToDateTime:String)
+ 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 : PositionalParameterNotFoun
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($wm i.LastBoot upTime)
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours
1
It works fine here. May be something issue is something else... not the script.
PS C:\Users\user> $wmi = get-wmiobject -class win32_OperatingSystem -computer user-pc
$LastRebootTime = $wmi.ConvertToDateTime($wm
[Timespan]$uptime = New-TimeSpan $LastRebootTime $(get-date)
$uptime.hours
1
It works fine here. May be something issue is something else... not the script.
ASKER
I can't get it to work just yet but it should be working.
ASKER
V-nas Thanks for the help. I gonna figur it out why it isn;'t working here.
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