PowerShell script issue
BSE technique
-
BSE technique -
BSE technique -
Hello,
I need a little help, I would like to set up two scripts but the first one is not working correctly.
The first script lists the accounts whose password is about to expire.
The second one shows a popup when the user logs in.
I need a little help, I would like to set up two scripts but the first one is not working correctly.
The first script lists the accounts whose password is about to expire.
import-module activedirectory # Retrieve today's date $date = Get-Date # Create the file $file="C:\Windows\SYSVOL\sysvol\"DOMAIN_NAME.lan"\scripts\users_expire.txt" Remove-Item $file -Force New-Item $file -ItemType file #ADD-content -path $file -value "sam;" ADD-content -path $file -value $date.DateTime $Listusers = Get-ADUser -SearchBase 'DC="DOMAIN_NAME", DC=lan' -Filter * Foreach ($user in $Listusers) { $foruser = $user.SamAccountName $Expiration = {[datetime]::FromFileTime((Get-ADUser –Identity $foruser -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed")} # Display dates in Ticks $exp = $expiration.Ticks $d = $date.Ticks # Calculate the difference in days $comp = $exp - $d # 12750000000000 = 15 days # If $comp is less than 15 days, write if ($comp -le 12750000000000) { ADD-content -path $file -value $foruser";" } } The second one shows a popup when the user logs in.
$connect = $env:USERNAME $file = Import-Csv -Delimiter ";" -Path "\\SRV01\SYSVOL\"DOMAIN_NAME.lan"\scripts\users_expire.txt" foreach ($line in $file) { $l = $line.sam if ($connect -eq $l) { $wshell = New-Object -ComObject Wscript.Shell $wshell.Popup("Please change your password Ctrl + Alt + Delete > Change a password Thank you") } } exit
2 answers
Here is the result of the first script in the file "users_expire.txt"
Thursday, March 16, 2017 12:07:11
Administrator;
Guest;
followed by all the usernames from the AD followed by a ";"
Thursday, March 16, 2017 12:07:11
Administrator;
Guest;
followed by all the usernames from the AD followed by a ";"
I made some modifications that are as follows:
In the text file, I have a good portion of the users followed by a ";" but I still don't have their password expiration.
I also encountered another problem during the modifications:
Exception calling "FromFileTime" with "1" argument(s): "Invalid Win32 FileTime.
Parameter name: fileTime"
Could there be a connection?
import-module activedirectory
# Retrieve today's date
$date = Get-Date
# Create the file
$file="C:\Windows\SYSVOL\sysvol\CMPP.lan\scripts\users_expire.txt"
Remove-Item $file -Force
New-Item $file -ItemType file
#ADD-content -path $file -value "sam;"
ADD-content -path $file -value $date.DateTime
$Listusers = Get-ADUser -SearchBase 'DC=CMPP, DC=lan' -Filter *
$expiration = 12750000000000
Foreach ($user in $Listusers)
{
$foruser = $user.SamAccountName
$proper = Get-ADUser -Identity $foruser -Properties "msDS-UserPasswordExpiryTimeComputed"
$value = $proper."msDS-UserPasswordExpiryTimeComputed"
if ($value -le $expiration) {
ADD-content -path $file -value $foruser";"
}
}
In the text file, I have a good portion of the users followed by a ";" but I still don't have their password expiration.
I also encountered another problem during the modifications:
Exception calling "FromFileTime" with "1" argument(s): "Invalid Win32 FileTime.
Parameter name: fileTime"
Could there be a connection?