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
}
}
| select -expand name
Usually a csv has the same column items on every line. – js2010