SCCM PowerShell 출력

SCCM PowerShell 출력

나는 컴퓨터가 마지막으로 부팅된 시간을 찾기 위해 PS 스크립트를 작성했습니다(Win10에서 빠른 부팅을 활성화했기 때문에 방금 시작한 것이 아니라 실제로 전원이 켜진 시간). 스크립트가 작업을 수행하는 것 같습니다. 부팅 날짜/시간, 마지막 사용자 및 호스트 이름을 다시 가져옵니다. 그러나 저는 이것을 SCCM에서 스크립트로 실행하고 있으며 대규모 출력을 처리하는 가장 좋은 방법을 확신하지 못합니다.

현재 스크립트는 해시 테이블로 출력되므로 결과를 읽는 사람을 위해 각 값에 키를 할당할 수 있습니다. 하지만 50대의 컴퓨터에 대해 이것을 실행한다면 결과를 스프레드시트나 정렬/필터링할 항목에 넣을 수 있기를 원할 것이라고 생각합니다. 그러나 해시 가능한 결과를 복사하면 여러 장치에서 형식이 제대로 지정되지 않습니다.

이것을 가장 잘 달성하는 방법에 대한 조언이 있는 사람이 있습니까? 저는 PowerShell 전문가와는 거리가 멀고 아래 내용 중 많은 부분이 비효율적이거나 중복될 가능성이 높다는 점을 주목할 가치가 있습니다. 또한 전체 이름에 대한 "(" 분할을 무시하십시오. 이는 사용자 계정의 형식과 관련이 있습니다.

미리 감사드립니다. 아래 스크립트의 현재 형태.

$LastBoot = Get-CimInstance -Class win32_operatingsystem | select lastbootuptime
$LastBoot = $LastBoot | Select -ExpandProperty "lastbootuptime"
$LastBoot = $LastBoot.ToString()

$LastBootDate = $LastBoot.split(" ")[0]

$LastBootTime = $LastBoot.split(" ")[1]

$LocalMachine = Get-CimInstance -ClassName win32_operatingsystem | select csname
$LocalMachine = $LocalMachine | Select -ExpandProperty "csname"

$Username = Get-CimInstance -Class win32_networkloginprofile | Sort-Object -Property LastLogon -Descending | Select -ExpandProperty "name" -First 1
$Username = $Username.split("\")[1]

$Domain = $env:userdomain

$Friendly = Get-CimInstance -Class win32_networkloginprofile | Sort-Object -Property LastLogon -Descending | Select -ExpandProperty "FullName" -First 1
$Friendly = $Friendly.split("(")[0]

$Results = @{
    Hostname = $LocalMachine;
    BootDate = $LastBootDate;
    BootTime = $LastBootTime;
    Username = $Username;
    User = $Friendly 
}

Write-Output $Results | Format-Table

답변1

결과를 CSV 파일로 내보내려면 다음을 사용하십시오.

$result.GetEnumerator() | Export-CSV "filepath.csv" -NoTypeInformation

또는 Excel 스프레드시트에 넣으려면 다음을 수행하세요.

$excel = new-Object -comobject Excel.Application
$excel.visible = $true # set it to $false if you don't need monitoring the actions...
$workBook = $excel.Workbooks.Add()
$sheet =  $workBook.Sheets.Item(1)
$sheet.Name = "Result"
$sheet.Range("A1","A2").ColumnWidth = 40
$sheet.range('A:A').VerticalAlignment = -4160 #align is center (TOP -4108 Bottom -4107 Normal)

$sheet.Cells.Item(1,1) = " "
$sheet.cells.Item(1,2) = " "

$index = 2

$result.keys | ForEach {  

    $sheet.Cells.Item($index,1) = $_
    $sheet.Cells.Item($index,2) = $result.item($_)
    $index++
}

$workBook.SaveAs("C:\mylist.xls")
$excel.Quit()

관련 정보