首页 > 解决方案 > 脚本不会读取多个用户

问题描述

我正在为我的团队编写一个脚本,以读取输入的用户列表并检查我们是否已为用户分配了一台机器。我的问题是当我运行脚本时,它只会读取放入记事本的单个用户,如果我放入多个用户,则不会生成任何内容。任何关于如何让它读取每个单独的输入的建议将不胜感激!

#Variables
$Inputfile = $env:HOMEDRIVE+"\temp\Users.txt"
$logfile = $env:HOMEDRIVE+"\temp\MachineMatch.csv"

#set variables, or deletes old ones. 
$testpath = Test-Path $logfile
if($testpath){del $logfile}


$testpath = $null
$testpath = Test-Path $Inputfile
if($testpath){del $Inputfile}

#open notepad and stores input information
Write-Host "Opening Notepad...`n"
sleep 2

Try
{
New-Item $Inputfile -ItemType file -ErrorAction Stop | Out-Null
Invoke-Item $Inputfile -ErrorAction Stop
}
catch
{
Write-Warning $_
Break
}


Write-Host "Do you wish to Proceed (Y/N): " -NoNewline
$choice = Read-Host

if($choice -ne "Y"){Break}
$list=$null
$list = Get-Content $Inputfile -ErrorAction Stop

$Get_All_Users = Get-AzureADUser -Filter "userPrincipalName eq '$list'"


ForEach($User in $Get_All_Users)
{
$User_ObjectID = $User.ObjectID 
$User_DisplayName = $User.DisplayName
$User_Mail = $User.UserPrincipalName
$User_Mobile = $User.Mobile
$User_OU = $User.extensionproperty.onPremisesDistinguishedName
$User_Account_Status = $User.AccountEnabled

$Get_User_Devices = (Get-AzureADUserRegisteredDevice -ObjectId $User_ObjectID)
$Count_User_Devices = $Get_User_Devices.count

$User_Owner_Obj = New-Object PSObject
$User_Owner_Obj | Add-Member NoteProperty -Name "User Name" -Value $User_DisplayName
$User_Owner_Obj | Add-Member NoteProperty -Name "User Mail" -Value $User_Mail -force
$User_Owner_Obj | Add-Member NoteProperty -Name "User OU" -Value $User_OU -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Account enabled ?" -Value $User_Account_Status 
$User_Owner_Obj | Add-Member NoteProperty -Name "Devices count" -Value $Count_User_Devices -force
#if we get a user device will provide the device information including if it's 20h2 or not, needing to use several
 
If($Count_User_Devices -eq 0)
{
$User_Owner_Obj | Add-Member NoteProperty -Name "Device name" -Value "No device" -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device last logon" -Value "No device" -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS type" -Value "No device" -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS version" -Value "No device" -force
}

If($Count_User_Devices -gt 1)
{
$Devices_LastLogon = @()
$Devices_OSType = @()
$Devices_OSVersion = @()
$Devices_DisplayName = @()

$Devices_LastLogon = ""
$Devices_OSType = ""
$Devices_OSVersion = ""
$Devices_DisplayName = ""

ForEach($Device in $Get_User_Devices)
{
$Device_LastLogon = $Device.ApproximateLastLogonTimeStamp
$Device_OSType = $Device.DeviceOSType
$Device_OSVersion = $Device.DeviceOSVersion
$Device_DisplayName = $Device.DisplayName

If ($owner -eq $Get_User_Devices[-1])
{
$Devices_LastLogon += "$Device_LastLogon" 
$Devices_OSType += "$Device_OSType"
$Devices_OSVersion += "$Device_OSVersion"
$Devices_DisplayName += "$Device_DisplayName"
}
Else
{
$Devices_LastLogon += "$Device_LastLogon`n" 
$Devices_OSType += "$Device_OSType`n"
$Devices_OSVersion += "$Device_OSVersion`n"
$Devices_DisplayName += "$Device_DisplayName`n"
}
}

$User_Owner_Obj | Add-Member NoteProperty -Name "Device name" -Value $Devices_DisplayName -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device last logon" -Value $Devices_LastLogon -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS type" -Value $Devices_OSType -force 
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS version" -Value $Devices_OSVersion -force
}
Else 
{
$Device_LastLogon = $Get_User_Devices.ApproximateLastLogonTimeStamp
$Device_OSType = $Get_User_Devices.DeviceOSType
$Device_OSVersion = $Get_User_Devices.DeviceOSVersion
$Device_DisplayName = $Get_User_Devices.DisplayName

$User_Owner_Obj | Add-Member NoteProperty -Name "Device name" -Value $Device_DisplayName -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device last logon" -Value $Device_LastLogon -force
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS type" -Value $Device_OSType -force 
$User_Owner_Obj | Add-Member NoteProperty -Name "Device OS version" -Value $Device_OSVersion -force

}
#user report to go into output CSV and gridview
$Users_report += $User_Owner_Obj
}
#output CSV file  
$Users_report | out-gridview        
$Users_report | export-csv "\temp\MachineMatch.csv" -notype -delimiter ";" 

标签: powershellinputazure-active-directoryazure-powershellintune

解决方案


如评论中所述,只需更改$Get_All_Users = Get-AzureADUser -Filter "userPrincipalName eq '$list'"$Get_All_Users = $list |ForEach-Object { Get-AzureADUser -Filter "userPrincipalName eq '$_'" }.


推荐阅读