get-WmiObject Last boot up time assistance please

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 4 months, 1 week ago.

  • Author
    Posts
  • #66654
    Profile photo of Cliff
    Cliff
    Participant

    I am looking for some assistance, or at least a point in the right direction. Still a bit new with PS scripting.

    I have a PS script to pull the LastBootUpTime from a remote host. I have a batch file first setting the execution policy to bypass to allow the script to run. The actual PS script then runs with elevated privileges, and prompts to enter the target computer name. The script runs successfully and the LastBootUpTime is displayed when the script exits. However if I try to modify the script to restart the function by using a do while, or starting a loop and then using an if statement, I find the output of the get-WmiObject is not displayed. It seems as though the data obtained from get-WmiObject is held until the script exits. Once the script exits, the data is then displayed.

    Is there a better way to retrieve the lastBootUpTime, and have it displayed on screen without the script completely exiting?

    Many Thanks in advance.

    function GetStartDate
    {
        $computername = read-host "Enter the computer name "
        get-WmiObject Win32_OperatingSystem -computername $computername |select CSname,@{label='lastBootUpTime' ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
        
        set-executionpolicy -scope process restricted
        
     }
    
  • #66660
    Profile photo of Albert van Boerum
    Albert van Boerum
    Participant

    Hi Cliff,

    Can you explain why you want to do the loop, while, etc.? Why do you want the script to continue running?

    Albert

  • #66666
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Did you know – you can run the Powershell console with the command line option -ep bypass? With that you don't need to actually change the execution policy inside the Powershell.
    I would start with something like this:

    function GetStartDate{
    	[CmdletBinding()]
    	param(
    		[Parameter(Position=0, Mandatory=$true,HelpMessage="Please enter the computer name ")]
    		[ValidateNotNullOrEmpty()]
    		[System.String]
    		$computername
    	)
        get-WmiObject Win32_OperatingSystem -computername $computername |select CSname,@{label='lastBootUpTime' ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
    }

    Now you have the function and you can loop it just like you need.

You must be logged in to reply to this topic.