1
votes

I'm new to powershell. Here is a script I am trying to write that, in a nutshell, will pull computers from AD into a variable. Take the list and iterate through each one and do the following:

  • test connection (online, offline)
  • test OSArchitecture
  • test for a subkey
  • write out the computer, connection status, subkey value to csv file

Every time I try to output to a file, whether out-file or export-csv, it doesn't come out correctly. it either puts 'Length' and value or some long string. I would ideally like to export to a csv so I can manipulate the data. The function 'DoesItemExist' is a funchtion to test if a registry exists. Here's the code:

#find computers names that start with H or D
$computers=Get-ADComputer -Filter {(name -like "H*") -or (name -like "D*")} -Properties     
Name |  select name
#save path to csv file
$SaveAs = 'c:\msolhelp\edocs.csv'
#loop through the computers collection
foreach($line in $computers)
{
    #test if computer is online
    if(Test-Connection -Cn $line -BufferSize 16 -Count 1 -ea 0 -quiet)
    {
    #write computer name to file

    #test registry path for 64 bit machine
    $OS=((Get-WmiObject Win32_Operatingsystem -ComputerName $line).OSArchitecture)
    if ($OS = '64-bit')

    #if 64 bit check for correct regkey and grab value and appends to file
    { 
        $regpath = 'SOFTWARE\Wow6432Node\'
        #check for subkey and append value to file
        $val = (DoesItemExist -path $regpath -regEntry "PatchLevel") 
        If ($val) { 
            Get-ItemProperty "HKLM:SOFTWARE\Wow6432Node\" | Select- 
            Object -Property PatchLevel | Export-Csv -Path $SaveAs -Append - 
            NoTypeInformation } 

        else {Get-ItemProperty "HKLM:SOFTWARE\Wow6432Node\" | 
         Select-Object -Property CurrentVersion | Export-Csv -Path $SaveAs -Append -
         NoTypeInformation}


    }

    #if false, it must be a 32 bit machine (different registry path)
    else 
    {
        #set path for 32 bit machine
        $regpath = 'HKLM\SOFTWARE\'
        #check for correct subkey
        $val = (DoesItemExist -path $regpath -regEntry "PatchLevel") 
        If ($val) { 
            Get-ItemProperty "HKLM:SOFTWARE\" | Select-Object -
            Property PatchLevel | Export-Csv -Path $SaveAs -Append -NoTypeInformation } 

        else {
            Get-ItemProperty "HKLM:SOFTWARE\" | Select-Object -
            Property CurrentVersion | Export-Csv -Path $SaveAs -Append -
            NoTypeInformation}


    }
}
#if test-connect fails, append file with 'offline'
else {
        "$line, offline" 
        continue
     }

}

1
The basic idea it seems is that you are running this script that will reach out to computers for information and that information will be written to file. The DoesItemExist function would be nice to have as since it does not appear your script is checking the remote registry of the computers in $computers. Just that of the computer running the script. Can you show some sample output that looks wrong so that we know know what you are trying to achieve?Matt
"Doesn't come out correctly" is an insufficient problem description. What output do you get, and how does that differ from the output you expect?Ansgar Wiechers
| select -expand name Usually a csv has the same column items on every line.js2010

1 Answers

0
votes

From what I can see you will want to save the variable to a text file, and then import-CSV later. Export-CSV doesn't actually do what you think it will...I suggest you read the link below..Export-CSV treats each item as a new row of data, which is why it doesn't look right. THE CSVs are determined by the object properties.

See documentation here: http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/23/use-powershell-to-work-with-csv-formatted-text.aspx