Battle Faction Puzzle 6

This topic contains 4 replies, has 2 voices, and was last updated by  Yubu 2 months, 3 weeks ago.

  • Author
    Posts
  • #93987

    Axel Bøg Andersen
    Participant

    A possible solution could be this:

    It works on Windows and Linux. It takes piped hostnames.

  • #93988

    Axel Bøg Andersen
    Participant

    Authentication drives me crazy. My work machine will only allow me to run Invoke-Command if I set Authentication to Basic. My private machine only wants whatever is default.

    winrm settings are alike on the two machines and firewall settings are correct. Luckily Linux doesn't care and runs the code smoothly.

    Under all circumstances ComputerName and Credential goes hand in hand. I added a Authentication parameter and changed the rule for adding ComputerName to the parameters for the Invoke-Command cmdlet.

    It seems to be a bit more tricky to get the uptime from the Linux machine remotely from the Windows box – furtunately that is not a request in the puzzle 😀

  • #94026

    Yubu
    Participant

    6.0: [pscustomobject]@{comp="server1","server2";OS="Windows"} | myuptime
    5.1: [pscustomobject]@{comp="server1","server2";OS="Windows"} | myuptime -credential (get-credential)
    Linux: works only locally.
    (gps init).startTime will give the alternative to boottime.

    function Get-MyUptime {
    	[CmdletBinding()]
    	[Alias("myuptime")]
    	param (
    		[Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=0)][array]$comp,
    		[Parameter(Mandatory=$false)][pscredential]$creds,
    		[Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]$OS
    	)
    	process {
    			
    		($comp) | %{
    			if ($PSVersionTable.OS -match "Linux") {$OS="Linux"} else {$OS="Windows"}
    			$props=[ordered]@{}
    			$cmp="$_"
    			if ($OS -match "Windows") {
    				if ($PSVersionTable.PSEdition -eq "Desktop") {
    					if (!$cmp) {$cmp="localhost"}
    					$sysinfo = gwmi Win32_OperatingSystem -comp $cmp -Credential $creds -ea SilentlyContinue
    					if ($sysinfo.LastBootUpTime) {$boottime=[System.Management.ManagementDateTimeconverter]::ToDateTime($sysinfo.LastBootUpTime)} 
    				}
    				else {
    					if (!$cmp) {$sysinfo = gcim Win32_OperatingSystem}
    					else {$sysinfo = icm -comp $cmp -Credential $creds {gcim Win32_OperatingSystem}}
    					$boottime=$sysinfo.LastBootUpTime
    				}
    			}
    			if ($OS -match "Linux") {
    				$boottime=get-date (uptime -s)
    				$hostname=hostname
    			}
    			$uptime=$(get-date)-$boottime
    			if ($OS -match "Windows") {$props.Host=$sysinfo.CSName}
    			if ($OS -match "Linux") {$props.Host=$hostname}
    			$props.BootTime=$boottime
    			$props.Uptime=$uptime
    			$props.UptimeDays=[math]::Round($uptime.totalDays,3)
    			$object=New-Object -TypeName psobject -Property $props
    			$object
    		}
    	}
    }
    
  • #94143

    Yubu
    Participant

    In params $comp and $OS ValueFromPipeline=$true, is missing by error.

  • #94581

    Yubu
    Participant

    This will work from Windows to Linux remotely with Powershell 6.0 and OpenSSH installed.

    get-date (icm -HostName $cmp -SSHTransport -ScriptBlock {uptime -s} -UserName user)
    

    ref: https://blogs.msdn.microsoft.com/powershell/2017/12/15/using-the-openssh-beta-in-windows-10-fall-creators-update-and-windows-server-1709/

You must be logged in to reply to this topic.