2015-01-20 3 views
1

내 스크립트에서 아래 코드 조각을 사용하여 원격 서버의 UPTIME을 검색하려고합니다. 내가 오류 메시지가 인수가 필요한 것입니다 결정 드릴 수 없습니다Powershell에서 원격 서버의 UPTIME 확인

Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values. 
Parameter name: dmtfDate" 

: 스크립트를 실행할 때

$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName  $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime 
$sysuptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) 
$uptime = " UPTIME   : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds" 

나는 아래의 오류?

감사합니다.

+0

'-ErrorAction SilentlyContinue' <-이 시나리오에서는 좋지 않은 아이디어이며 문제가 될 수도 있습니다. 자동으로 null 값을 변환하려고 시도합니다. 실패하면 명령에 오류가 있어야합니다. 그렇지 않으면 나머지 명령이 실패합니다. – arco444

+0

이 코드는 정상적으로 작동합니다. arco444에 동의합니다. '$ lastboottime'은 이것이 실패 할 때 null 일 가능성이 높습니다. 'ToDateTime ($ null)'은 그 erorr를 생성합니다. – Matt

답변

0

당신은 가능성이 가장 높은 첫 번째 줄에서 널을 얻고있다. Arco444가 그의 코멘트에서 보여 주듯이, 당신에게 통보하지 않고 처리를 실패하면 명령에 명령을 내리고 있습니다. 실패하면 $lastboottime은 $ null이됩니다. 목적에 따라 $null을 입력하면 비슷한 오류가 발생할 수 있습니다.

System.DirectoryServices.dll[System.Management.ManagementDateTimeconverter]::ToDateTime($null) 

System.DirectoryServices.dll [System.Management.ManagementDateTimeconverter] :: ToDateTime

는 : 용어 'System.DirectoryServices.dll는 [System.Management.ManagementDateTimeconverter] :: ToDateTime' 인식되지 cmdlet, 함수, 스크립트 파일 또는 실행 가능 프로그램의 이름. 이름의 철자를 확인하거나 경로에 이 포함되어 있으면 경로가 올바른지 확인하고 다시 시도하십시오. 행에서 1 문자 : 1 + [System.Management.ManagementDateTimeconverter] :: ToD 정보 System.DirectoryServices.dll ... + ~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound (System.Director ... ER] :: ToDateTime : 문자열)] CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

간단한을 변수에 데이터가 있는지 검사합니다. 그리고 $uptime 값도이를 반영합니다.

$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName  $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime 
If($lastboottime){ 
    $sysuptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime) 
    $uptime = " UPTIME   : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds" 
} Else { 
    $uptime = " UPTIME   : Unable to determine for host $server" 
} 
4

WMI 개체에서 datetime 개체로 시간 값 변환은 개체 자체에서 ConvertToDateTime 메서드를 호출하여 수행 할 수 있습니다.

간단한 예 :

$wmi = Get-WMIObject -Class Win32_OperatingSystem 
$lastboottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime) 
$sysuptime = (Get-Date) - $lastboottime 
$uptime = " UPTIME   : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds" 
write-host $uptime