I am trying to create a script which searches a list of computers/servers and identifies if a user profile exists for each member of an AD group called 'TestDisabledUsers'. Results are then piped to a CSV file with the format below.
ComputerName WMI_Connection Pingable Profile_Search
Computer1 Server IS Contactable TRUE User1 Exists ; User2 Exists ; User3 No Profile
Computer2 Server IS Contactable TRUE User1 No Profile ; User2 Exists ; User3 Exists
Computer3 Server IS Contactable TRUE User1 Exists ; User2 Exists ; User3 No Profile
Current script is not working at present as it only shows 1 user in the Profile_Search
output :-(
Apologies if this is a simple solution, I'm not the best coder ;-) Any help very much appreciated.
So far I have the following powershell script :-
Clear-History
$ErrorActionPreference= 'silentlycontinue'
$outputFolderName = 'ProfileAudit ' $(Get-Date -f dd-MM-yyyy)
$outputpath = "C:\temp\$outputFolderName"
If(!(test-path $outputpath))
{
New-Item -ItemType Directory -Force -Path $outputpath | out-null
}
$computers = Get-Content -path C:\Temp\svrs.txt
$report = @()
foreach ($computer in $computers) {
$Ping = Test-Connection -ComputerName $computer -Quiet -count 2
$wmi = gwmi win32_bios -ComputerName $computer
if ($wmi)
{
$WMIResult = 'Server IS Contactable'
$profiles = Get-ADGroupMember -Identity TestDisabledUsers | Foreach {$_.SamAccountName}
foreach ($profile in $profiles) {
$user = Get-CimInstance -ComputerName $computer -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $profile }
if ($user)
{
$profileexists = ("$profile Exists") -join ' ; '
#$user | Remove-CimInstance
}
else {
$profileexists = ("$profile No Profile") -join ' ; '
}
}
$tempreport = New-Object -TypeName PSObject
$tempreport | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$tempreport | Add-Member -MemberType NoteProperty -Name WMI_Connection -Value $WMIResult
$tempreport | Add-Member -MemberType NoteProperty -Name Pingable -Value $Ping
$tempreport | Add-Member -MemberType NoteProperty -Name Profile_Search -Value $profileexists
$report = $tempreport
}
else
{
$WMIResult = 'Server NOT Contactable'
$tempreport = New-Object PSObject
$tempreport | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$tempreport | Add-Member -MemberType NoteProperty -Name WMI_Connection -Value $WMIResult
$tempreport | Add-Member -MemberType NoteProperty -Name Pingable -Value $Ping
$tempreport | Add-Member -MemberType NoteProperty -Name Profile_Search -Value $null
$report = $tempreport
}
}
$CSVFileName = 'ProfileAudit ' $(Get-Date -f dd-MM-yyyy) '.csv'
$report | Export-Csv $outputpath\$CSVFileName -NoTypeInformation
CodePudding user response:
Try the below. The main issue is that you're overwriting $profileexists instead of appending to it in the profile loop.
Clear-History
$outputpath = "C:\temp\ProfileAudit $(Get-Date -f dd-MM-yyyy)"
If(!(test-path $outputpath))
{
New-Item -ItemType Directory -Force -Path $outputpath | out-null
}
$report = Get-Content -path C:\Temp\svrs.txt | % {
$ping = Test-Connection -ComputerName $_ -Quiet -count 2
$profileexists = $null
if(gwmi win32_bios -ComputerName $_)
{
$WMIResult = 'Server IS Contactable'
$profiles = Get-ADGroupMember -Identity TestDisabledUsers | select -ExpandProperty SamAccountName
foreach ($profile in $profiles) {
$user = Get-CimInstance -ComputerName $_ -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $profile }
if ($user)
{
$profileexists = "$profile Exists;"
}
else {
$profileexists = "$profile No Profile;"
}
}
}
else
{
$WMIResult = 'Server NOT Contactable'
}
[pscustomobject]@{
ComputerName = $_.ToUpper()
WMI_Connection = $WMIResult
Pingable = $Ping
Profile_Search = $profileexists
}
}
$CSVFileName = 'ProfileAudit ' $(Get-Date -f dd-MM-yyyy) '.csv'
$report | Export-Csv $outputpath\$CSVFileName -NoTypeInformation