0

I'm trying to determine how to properly call a variable for Get-WmiObject using a list of computers.

No matter what I do I cannot get this code to work.

I have spent a few hours reviewing code to compile the code I have and feel like I'm 90% there but can't get past this last little thing. I'm also new to Powershell but am trying my butt off

$comp = Import-CSV .\testlist.csv 

foreach $comp{



    $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $comp
    $bios = Get-CimInstance -ClassName Win32_BIOS -ComputerName $comp
    $tpm = Get-WmiObject -Namespace root\cimv2\security\microsofttpm -Class Win32_Tpm -computername $comp -Authentication PacketPrivacy
    $cos = Get-CimInstance -ClassName Win32_OperatingSystem -computername $comp | select-object Caption, BuildNumber, OSArchitecture

    $properties = [ordered]@{
        'ComputerName' = $comp;
        'Model' = $cs.Model;
        'Operating System' = $cos.Caption
        'OS build' = $cos.BuildNumber
        'OS Type' = $cos.OSArchitecture
        'BIOSVersion' = $bios.SMBIOSBIOSVersion
        'TPM ManufacturerId' = $tpm.ManufacturerId
        'TPM Firmware version' = $tpm.ManufacturerVersion
        'TPM SpecVersion' = $tpm.SpecVersion
        )


}| Export-Csv -Path .\Result.csv -NoTypeInformation

Error Message:

New Text Document.ps1:4 char:8 + foreach $comp{ + ~ Missing opening '(' after keyword 'foreach'.

New Text Document.ps1:4 char:14 + foreach $comp{ + ~ Unexpected token '{' in expression or statement.

New Text Document.ps1:22 char:39 + 'TPM SpecVersion' = $tpm.SpecVersion + ~ The hash literal was incomplete.

New Text Document.ps1:4 char:14 + foreach $comp{ + ~ Missing closing '}' in statement block or type definition.

New Text Document.ps1:23 char:3 + ) + ~ Unexpected token ')' in expression or statement.

New Text Document.ps1:26 char:1 + }| Export-Csv -Path .\Result.csv -NoTypeInformation + ~ Unexpected token '}' in expression or statement.

New Text Document.ps1:26 char:2 + }| Export-Csv -Path .\Result.csv -NoTypeInformation + ~

An empty pipe element is not allowed. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

HAL9256
  • 12,384
  • 1
  • 34
  • 46
Reckless
  • 3
  • 2
  • remove the parenthesis after $tpm.SpecVersion – Kory Gill May 30 '19 at 22:49
  • [1] PLEASE fix your code formatting. ///// [2] this `foreach $comp{` is NOT how one calls a foreach loop. [*grin*] it should be something like `foreach ($Thing in $Collection) {Do-Stuff -With ParameterInfo}`. – Lee_Dailey May 30 '19 at 23:16

1 Answers1

0

Some basic things I have changed:

  • ForEach : if you don't use the () you can loop through the $comp using Foreach-Object:

$comp | ForEach-Object { write-host "computername: $_ "} .

  • No ; at the end of the line :)

  • Pipe every element to export csv and use -append (you were piping the final result)

  • I prefer to use convertTo-csv | out-file somefile.csv . Export-csv is doing the same but applies some filters on the output that will sometimes make unexpected results

foreach ($c in $comp) {
            $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $c
            $bios = Get-CimInstance -ClassName Win32_BIOS -ComputerName $c
            $tpm = Get-WmiObject -Namespace root\cimv2\security\microsofttpm -Class Win32_Tpm -computername $c -Authentication PacketPrivacy
            $cos = Get-CimInstance -ClassName Win32_OperatingSystem -computername $c | select-object Caption, BuildNumber, OSArchitecture    
        $properties = [ordered]@{
            'ComputerName'         = $c
            'Model'                = $cs.Model
            'Operating System'     = $cos.Caption
            'OS build'             = $cos.BuildNumber
            'OS Type'              = $cos.OSArchitecture
            'BIOSVersion'          = $bios.SMBIOSBIOSVersion
            'TPM ManufacturerId'   = $tpm.ManufacturerId
            'TPM Firmware version' = $tpm.ManufacturerVersion
            'TPM SpecVersion'      = $tpm.SpecVersion
        }  | 
        Export-Csv -Path .\Result.csv -NoTypeInformation
}

After the } | there is a back-tick character ( ` ) that tells powershell the line is broken in two . It was messing the formatting, so i removed it, don't forget to put it back.

TudorIftimie
  • 1,050
  • 11
  • 21