2017-11-22 13 views
1

このスクリプトでは、ローカルマシンの証明書に関する情報を収集し、証明書の期限が切れる日数をJSONで表示します。私が必要powershellスクリプトの編集方法がわかりません

{ "{#CERTINFO}" : "@{Expires in (Days)=8074}" } 

8074(日)を必要とし、このスクリプトを編集する方法を見つけ出すことはできませんです。

$cert_ = get-childitem cert:LocalMAchine -recurse | 
where-object {$_.NotAfter -gt (get-date)} | 
select @{Name="Expires in (Days)";Expression={($_.NotAfter).subtract([DateTime]::Now).days}} | 
Sort "Expires in (Days)" 

write-host "{" 
write-host " `"data`":[`n" 
$idx = 1 
foreach ($cert_arr in $cert_) 
{ 
if ($idx -lt $cert_.Count) 

{ 
$line= "{ `"{#CERTINFO}`" : `"" + $cert_arr + "`" }," 
write-host $line 
} 
elseif ($idx -ge $cert_.Count) 
{ 
$line= "{ `"{#CERTINFO}`" : `"" + $cert_arr + "`" }" 
write-host $line 
} 
$idx++; 
} 
write-host 
write-host " ]" 
write-host "}" 
+1

これは、PowerShellを書くにはかなりひどい方法です。おそらく、あなたは単にやり直すべきです(そしてその過程で、言語について何かを学んでください)。 – Joey

+0

何を試しましたか?すべてのwrite-hostステートメントを削除することから始めます。 –

+0

ええ、それは理解している:> –

答えて

0

私がしようとするだろう:

function Get-CertInfo 
 
{ 
 

 
    $cert_ = get-childitem cert:LocalMAchine -recurse | where-object {$_.NotAfter -gt (get-date)} | select @{Name="Expires in (Days)";Expression={($_.NotAfter).subtract([DateTime]::Now).days}} | Sort "Expires in (Days)" 
 

 
    $data = @() 
 

 
    foreach($c in $cert_) 
 
    { 
 
     $entry = [PSObject]@{"#CERTINFO"="$c"} 
 

 
     $data += $entry 
 
    } 
 

 
    $certInfor = [PSObject]@{"data"=$data} 
 

 
    $certInfor | ConvertTo-Json 
 
}

生成する:

{ 
"data": [ 
      { 
       "#CERTINFO": "@{Expires in (Days)=221}" 
      }, 
      { 
       "#CERTINFO": "@{Expires in (Days)=272}" 
      } 
     ] 
} 

に役立ちます願っています。

関連する問題