我正在嘗試創建一個每天將通過任務執行的 PowerShell 腳本。該任務將檢查用戶是否成為 AD 組的成員超過 7 天。如果是這樣,該用戶將被洗掉。資料是從 CSV 檔案中匯入的,我們將在其中插入用戶添加到組的日期:
samaccountname,Date
user 1,20/01/2022
user2,06/02/2022
這是我寫的代碼:
$users = "C:\Path to CSV\File.csv"
Import-Module ActiveDirectory
Import-Csv $users
foreach ($user in $Users)
{
$CheckDate2 = get-date ($user.'Date').AddDays(7)
$CheckDate1 = get-date ($user.'Date')
if ($CheckDate1 -lt $CheckDate2){
exit
}
else{
Remove-ADGroupMember -Identity "Group Name" -Members $user.samaccountname -Confirm:$false
}
}
這是我收到的錯誤:
You cannot call a method on a null-valued expression.
At line:8 char:1
$CheckDate2 = get-date ($user.'Date').AddDays(7)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type "System.DateTime"."
At line:9 char:24
$CheckDate1 = get-date ($user.'Date')
~~~~~~~~~~~~~~
CategoryInfo : WriteError: (:) [Get-Date], ParameterBindingException
FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand
Remove-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:16 char:68
... tity "Group Name" -Members $user.samaccountname -Confir ...
~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidData: (:) [Remove-ADGroupMember], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
uj5u.com熱心網友回復:
您的foreach回圈正在列舉$users,其值為"C:\Path to CSV\File.csv"。這意味著將有一個回圈迭代,其中$user的值為"C:\Path to CSV\File.csv". 由于[String]該類沒有Date屬性,因此...
($user.'Date').AddDays(7)
......實際上是這個......
($null).AddDays(7)
...因此錯誤。您也沒有對 的輸出做任何事情Import-Csv $users,因此您需要將其收集到某個地方進行處理;要么將其存盤在變數中...
$usersPath = "C:\Path to CSV\File.csv"
Import-Module ActiveDirectory
$users = Import-Csv $usersPath
foreach ($user in $users)
{
# ...
...或者,更好的是,直接回圈輸出以在讀取記錄時處理記錄...
$usersPath = "C:\Path to CSV\File.csv"
Import-Module ActiveDirectory
foreach ($user in Import-Csv $usersPath)
{
# ...
為了清楚起見,我將您的初始$users變數重命名$usersPath為;以這種方式重用變數可能會令人困惑和意外。
即使在修復回圈之后,您仍然需要從中更改括號......
get-date ($user.'Date').AddDays(7)
……到這……
(get-date $user.'Date').AddDays(7)
...to create a [DateTime] using the value of $user.'Date' and then call AddDays(7) on the result. Note that you don't need to quote a property that doesn't contains spaces in the name, so you can simplify that to...
(get-date $user.Date).AddDays(7)
Also, you are comparing a user's Date with Date 7d, which will always be $true. Instead, you want to test if today is beyond the 7-day membership limit...
$today = (Get-Date).Date # Ignore the TimeOfDay portion so the expiration happens on a day boundary
$userAddedDate = Get-Date ($user.Date)
$maxGroupMembershipTime = New-TimeSpan -Day 7
if ($today - $userAddedDate -gt $maxGroupMembershipTime)
# Alternative: if ($userAddedDate $maxGroupMembershipTime -lt $today)
{
# ...
Finally, you are conditionally exiting the loop, which means no further user records nor anything else in the script will be processed. To skip that user you would replace exit with continue, or just let control fall through to the end of the foreach block...
$usersPath = "C:\Path to CSV\File.csv"
$today = (Get-Date).Date # Ignore the TimeOfDay portion so the expiration happens on a day boundary
$maxGroupMembershipTime = New-TimeSpan -Day 7
Import-Module ActiveDirectory
foreach ($user in Import-Csv $usersPath)
{
$userAddedDate = Get-Date ($user.Date)
if ($today - $userAddedDate -gt $maxGroupMembershipTime)
# Alternative: if ($userAddedDate $maxGroupMembershipTime -lt $today)
{
Remove-ADGroupMember -Identity "Group Name" -Members $user.samaccountname -Confirm:$false
}
}
I moved the initialization of $today outside of the loop since there's really no need to set it on each iteration and to ensure every user gets the same date for "today" regardless of when the script executes.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/423415.html
標籤:
