2014-07-16 159 views
1

我是新来的PowerShell。下面是我正在尝试写的一个脚本,简而言之,它将把AD中的计算机变成一个变量。就拿列表,并通过每一个迭代,并做到以下几点:PowerShell - 将变量导出到csv文件

  • 测试连接(在线,离线)
  • 测试OSArchitecture
  • 测试的子项
  • 写出来的电脑,连接状态,子项值csv文件

每次我尝试输出到文件,无论是out-file或export-csv,它都不能正确输出。它要么把'长度'和价值或一些长字符串。我最好喜欢导出到csv,以便我可以操纵数据。函数'DoesItemExist'是测试注册表是否存在的一个函数。下面的代码:

#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 
    } 

}

+0

似乎基本思想是,运行此脚本,将接触到的信息,计算机和信息将被写入文件。 DoesItemExist函数会很好,因为它没有出现您的脚本正在检查$计算机中的计算机的远程注册表。就是运行该脚本的计算机。 你可以显示一些看起来不对的示例输出,以便我们知道你想要实现什么吗? – Matt

+0

“不能正确显示”是不充分的问题描述。你得到了什么输出,这与你所期望的输出有什么不同? –

回答