Author Posts

March 17, 2017 at 1:48 am

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
    
 }

March 17, 2017 at 8:37 am

Hi Cliff,

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

Albert

March 17, 2017 at 9:41 am

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.