Home > Software design >  How to output from Powershell with multiple variables
How to output from Powershell with multiple variables

Time:11-02

I need your help on the following script. Before we start I know it's not the prettiest script, I'm a beginner.

How can I output it to one CSV and not into three files for each variable? ($DomAdmins, $OrgAdmins & $Server)

Script:

#Module
    #Import-Module activedirectory 

#Variablen
    $Date= get-date -format "yyyy/MM/dd"
    cls
    $MA= Read-Host -Prompt "MA eingeben:"
    cls
    $Kunde= Read-Host -Prompt "Kundenkürzel eingeben:"
    cls
    $Path=  Read-Host -Prompt "Speicherpfad eingeben (z.B: C:\:"
    cls
    $fullpath=$Path "" $Kunde "_" $Date "_" "$MA" ".csv"
#Admins
    $DomAdmins=get-adgroupmember 'Domänen-Admins' | ft SamAccountName 
    $OrgAdmins=get-adgroupmember 'Organisations-Admins' | ft SamAccountName 
#Server    
    $Server=Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' `
    -Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address  |
    Sort-Object -Property Operatingsystem |
    Select-Object -Property Name,Operatingsystem,OperatingSystemVersion,IPv4Address 
#Output
    cls
    Write-Host "Kundendoku vom: " $Date -ForegroundColor cyan
    Write-Host "Autor: " $MA 
    Write-Host "Kundenkürzel:" $Kunde 
    Write-Host "" -ForegroundColor cyan
    Write-Host "Alle Domänenadmins:" -ForegroundColor cyan
    $DomAdmins | FT | Out-String|% {write-host $_ }
    Write-Host "Alle Organisationsadmins:" -ForegroundColor cyan
    $OrgAdmins | FT | Out-String|% {write-host $_ }
    Write-Host "Alle Domänenserver" -ForegroundColor cyan
    Write-Host "Achtung! Nur Domänenserver!" -ForegroundColor red
    $Server | FT | Out-String|% {write-host $_ }

Thank you for your help, BR Michael

CodePudding user response:

You can do something like this:

$filePath = "C:\output.csv"
#first use of Out-file to csv file destroys previously existing file if it existed because -Append is not used
$DomAdmins | Out-File -FilePath $filePath
#next uses of Out-File use -Append so content is added
$OrgAdmins | Out-File -FilePath $filePath -Append
$Server | Out-File -FilePath $filePath -Append
  • Related