Try specifying $g first.

$g = 0

Or you may have to declare as an array

$g = @()

Daniel Ratliff

From: [email protected] [mailto:[email protected]] On 
Behalf Of Robertson, Casey
Sent: Wednesday, June 11, 2014 8:10 PM
To: [email protected]
Subject: [mssms] Very rusty with scripting and doing custom inventory

Stepped away from SCCM for a while but used to leverage VBscripts all the time 
to query various things and drop them into WMI.  Now that I'm back in the SCCM 
world I'd like to do this again but with PowerShell.  Having a devil of a time. 
 I found Sherry and Matthew's script here:

http://www.sccm-tools.com/tools/vbscript/vbscript-certificates.html

I wanted to convert it to Powershell but I'm striking out on the initial array. 
 This is the code in VBScript:


Store.Open CAPICOM_LOCAL_MACHINE_STORE, "MY" ,CAPICOM_STORE_OPEN_READ_ONLY

Set Certificates = Store.Certificates

If Certificates.Count >0 Then
   For Each Certificate in Certificates
       g=g+1
       strSubjectName(g) = Certificate.SubjectName
       strIssuerName(g) = Certificate.IssuerName
       strValidFrom(g) = Certificate.ValidFromDate
       strValidTo(g) = Certificate.ValidToDate
       strDaysToExpire(g) = DateDiff("d",now(),Certificate.ValidToDate)


This is how I'm trying to do it in Powershell but I keep getting an error about 
"Cannot index into a null array".  Any help?  Sorry for being a poor scripter  
:)

$Certificates = Get-ChildItem -Path cert:\LocalMachine\My

If ($Certificates.Count -gt 0)
   {ForEach ($Certificate in $Certificates)
       {
        $g++
       $strSubjectName[$g] = $Certificate.SubjectName
       $strIssuerName[$g] = $Certificate.IssuerName
       $strValidFrom[$g] = $Certificate.NotBefore
       $strValidTo[$g] = $Certificate.NotAfter
       $strDaysToExpire[$g] = New-TimeSpan -End 
([DateTime]$Certificate.NotAfter)
         $strDaysToExpire[$g] = $($strDaysToExpire.days)

         }
         }

Script probably has other issues too but one step at a time

Thanks
Casey


The information transmitted is intended only for the person or entity to which 
it is addressed
and may contain CONFIDENTIAL material.  If you receive this 
material/information in error,
please contact the sender and delete or destroy the material/information.


Reply via email to