저는 PowerShell을 처음 사용합니다. 간단히 말해서 AD에서 변수로 컴퓨터를 가져 오는 스크립트를 작성하려고합니다. PowerShell - 변수를 CSV 파일로 내보내기
- 테스트 연결
- 테스트 OSArchitecture
- 시험 : 다음 목록을 가지고 각 하나를 통해 반복하고 할 값을 CSV 파일로 변환
out-file이든 export-csv이든 파일에 출력하려고 할 때마다 올바르게 출력되지 않습니다. '길이'와 값 또는 긴 문자열을 넣습니다. 데이터를 조작 할 수 있도록 CSV로 내보내는 것이 이상적입니다. 'DoesItemExist'함수는 레지스트리가 있는지 테스트하는 데 사용됩니다. 여기 코드는 : 나는 당신이 텍스트 파일에 변수를 저장할 것보고, 그리고 나중에 가져 오기 CSV 수있는 일부터
#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
}
}
보인다 기본적인 아이디어는 당신이 정보를 컴퓨터에 밖으로 도달 할 것이 스크립트를 실행하고 그 정보가 파일에 기록 될 것입니다. DoesItemExist 함수는 스크립트가 $ computers에있는 컴퓨터의 원격 레지스트리를 확인하는 것으로 보이지 않으므로 as가 좋을 것입니다. 스크립트를 실행하는 컴퓨터의 것입니다. 우리는 당신이 달성하고자하는 것을 알기 위해 잘못된 샘플 출력을 보여줄 수 있습니까? – Matt
"제대로 나오지 않습니다"는 문제 설명이 충분하지 않습니다. 어떤 산출물을 얻고, 예상되는 산출물과 어떻게 다른가? –