Hi All,<\/p>\n
I’m trying to modify one of my scripts on checking user’s accounts lock status, made in Windows Forms. There’s a text box where people can put User’s Display Name / Name and then a button that says Check Locked<\/p>\n
We have some users who don’t have a Lastname set up in the system (this because they are configured like that) and I have if condition to check if the textbox contains space does make the split otherwise don’t make the split.<\/p>\n
Here’s the current code:<\/p>\n
function CheckLocked {\n\n$Domains = @(\n'DomainA'\n'DomainB'\n'DomainC'\n)\n \nif ($User.text -like '* *'){\n $FirstName, $LastName = $User.text.Split(' ')[0,-1]\n $User = $FirstName + '.' + $LastName\n \n} else { }\n\nForeach ($Domain in $Domains) {\n\n Try {\n \n $Result = Get-ADUser -Identity $User -Server $Domain -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled\n \n } Catch {\n \n $Result | Out-GridView -Title 'Locked Accounts'\n \n }\n} \n}\n<\/code><\/pre>\nHowever, when I fill up a name it gets stuck on the beginning of the ForEach<\/p>\n
DEBUG: ! SET $User = ‘John.Smith’.
\nDEBUG: 136+ Foreach ($Domain in >>>> $Domains) {<\/p>\n
DEBUG: ! SET $foreach = ’ DomainA DomainB Domain…'.
\nDEBUG: 136+ Foreach ( >>>> $Domain in $Domains) {<\/p>\n
DEBUG: ! SET $foreach = ‘’.
\nDEBUG: 148+ >>>> }<\/p>\n
DEBUG: 104+ $CheckLocked.Add_Click({ CheckLocked >>>> })<\/p>\n
Can anyone tell where I messed up and how can I fix it?<\/p>\n
Thank You All.<\/p>","upvoteCount":4,"answerCount":7,"datePublished":"2020-05-20T05:51:56.000Z","author":{"@type":"Person","name":"kalin38","url":"https://community.spiceworks.com/u/kalin38"},"acceptedAnswer":{"@type":"Answer","text":"
You are not putting the ‘$user’ variable in the function scope<\/p>\n
you need to use param() to get that data
\ne.g.<\/p>\n
function CheckLocked {\nparam(\n $user\n)\n $Domains = 'DomainA','DomainB','DomainC'\n # more stuff in your function\n}\n\n# call function\nCheckLocked -user $user\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-05-20T05:59:37.000Z","url":"https://community.spiceworks.com/t/script-stuck-on-beginning-of-foreach/763272/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi All,<\/p>\n
I’m trying to modify one of my scripts on checking user’s accounts lock status, made in Windows Forms. There’s a text box where people can put User’s Display Name / Name and then a button that says Check Locked<\/p>\n
We have some users who don’t have a Lastname set up in the system (this because they are configured like that) and I have if condition to check if the textbox contains space does make the split otherwise don’t make the split.<\/p>\n
Here’s the current code:<\/p>\n
function CheckLocked {\n\n$Domains = @(\n'DomainA'\n'DomainB'\n'DomainC'\n)\n \nif ($User.text -like '* *'){\n $FirstName, $LastName = $User.text.Split(' ')[0,-1]\n $User = $FirstName + '.' + $LastName\n \n} else { }\n\nForeach ($Domain in $Domains) {\n\n Try {\n \n $Result = Get-ADUser -Identity $User -Server $Domain -Properties Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled | select Name, LastLogonDate, LockedOut, AccountLockOutTime, Enabled\n \n } Catch {\n \n $Result | Out-GridView -Title 'Locked Accounts'\n \n }\n} \n}\n<\/code><\/pre>\nHowever, when I fill up a name it gets stuck on the beginning of the ForEach<\/p>\n
DEBUG: ! SET $User = ‘John.Smith’.
\nDEBUG: 136+ Foreach ($Domain in >>>> $Domains) {<\/p>\n
DEBUG: ! SET $foreach = ’ DomainA DomainB Domain…'.
\nDEBUG: 136+ Foreach ( >>>> $Domain in $Domains) {<\/p>\n
DEBUG: ! SET $foreach = ‘’.
\nDEBUG: 148+ >>>> }<\/p>\n
DEBUG: 104+ $CheckLocked.Add_Click({ CheckLocked >>>> })<\/p>\n
Can anyone tell where I messed up and how can I fix it?<\/p>\n
Thank You All.<\/p>","upvoteCount":4,"datePublished":"2020-05-20T05:51:56.000Z","url":"https://community.spiceworks.com/t/script-stuck-on-beginning-of-foreach/763272/1","author":{"@type":"Person","name":"kalin38","url":"https://community.spiceworks.com/u/kalin38"}},{"@type":"Answer","text":"
welcome<\/p>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n