2016-10-21 10 views
0

この関数は、リモートPC上にインストールされているソフトウェアのリストを取得します。この関数の特殊な特長は、日付をyyyyMMddからMM/dd/yyyyに変換することです。この関数が機能している間に、InstallDateのオブジェクトを削除しています。私はこのエラーを取得する:パイプラインでのエラー処理

Exception calling "ParseExact" with "3" argument(s).

function Get-Software { 
    param ([string[]]$ComputerName) 

    [scriptblock]$code = { 
     $keys = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 
       'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' 

     Get-ItemProperty -Path $keys | 
      Where-Object { $_.DisplayName -ne $null } | 
      Where-Object { $_.DisplayName -notlike '*update*' } | 
      sort DisplayName | 
      ForEach-Object { 
       [PSCustomObject]@{ 
        Display = $_.DisplayName; 
        Installed = Get-Date ([DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)) -format d 
       } 
      } 
    } 
    Invoke-Command -ScriptBlock $code -ComputerName $ComputerName -ArgumentList * | 
     ft Display, Installed -AutoSize | 
     Out-File "$Env:USERPROFILE\Desktop\SWinv.txt" 
} 

Get-Software RemotePC 

私はすべてのオブジェクトを返すために何ができますか?

+0

正確にはどのような '$ _。インスタllDate'はいつもあなたのインストール日はありませんか?_空/ヌルですか? 0? – gravity

答えて

1

nullにすることができ、日付をインストールするので、してみてください。この

 function Get-Software { 
     param ([string[]]$ComputerName) 

     [scriptblock]$code = { 
      $keys = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 
        'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' 

      Get-ItemProperty -Path $keys | 
       Where-Object { $_.DisplayName -ne $null -and $_.DisplayName -notlike '*update*' } | 
       sort DisplayName | 
       ForEach-Object { 
        [PSCustomObject]@{ 
         Display = $_.DisplayName; 
         Installed =if ($_.InstallDate -ne $null) { Get-Date ([DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)) -format d} else {$null} 
        } 
       } 
     } 
     Invoke-Command -ScriptBlock $code -ComputerName $ComputerName -ArgumentList * | 
      ft Display, Installed -AutoSize | 
      Out-File "$Env:USERPROFILE\Desktop\SWinv.txt" 
    } 

    Get-Software RemotePC 
+0

私はパイプラインでtry..catchを使ったことが一度もなかったので、私は考慮しませんでした。(インストールされていない場合)それ。ありがとう! – Dastechguy

1

単純なif/elseチェックを入れることができます。 $ _と仮定すると

あなたがエラーを取得するときInstallDateがnull:。

ForEach-Object { 
    if ($null -ne $_.InstallDate) { 
     [PSCustomObject]@{Display = $_.DisplayName; Installed = Get-Date ([datetime]::ParseExact($_.InstallDate,'yyyyMMdd',$null)) -format d} 
    } else { 
     [PSCustomObject]@{Display = $_.DisplayName; Installed = "Not Available"} 
    } 
} 
+0

また、オブジェクトの作成と条件付きを逆にすることもできます。 '[PSCustomObject] @ {...;}のようになります。 –

1

は、try..catchで日付の解析を行い、結果を変数に割り当て、オブジェクトを作成するときにその変数を使用します。

... | ForEach-Object { 
    $date = try { 
     [DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null).ToString('d') 
    } catch { 
     '' # default value to assign if date is invalid 
    } 
    [PSCustomObject]@{ 
     Display = $_.DisplayName; 
     Installed = $date 
    } 
}