So I broke down and got temp approval for testing purposes to use DelProf2. I have a function written for this:
function DelProf { param( [parameter(mandatory=$true)] [string[]]$computername ) foreach($computer in $computername) { & "C:DelProf2.exe" /l /c:$computer Write-Host "`n`nPLEASE ENSURE YOU FULLY UNDERSTAND THIS COMMAND BEFORE USE `nTHIS WILL DELETE ALL USER PROFILE INFORMATION FOR SPECIFIED USER(S) ON THE SPECIFIED WORKSTATION!`n" $DeleteUsers = Read-Host -Prompt "To delete User Profiles, please use the following syntax ; Wildcards (*) are accepted. `nExample: /id:user1 /id:smith* /id:*john*`n `nEnter proper syntax to remove specific users" if([string]::IsNullOrWhiteSpace($DeleteUsers)) { Write-Host "`nImproper value entered, excluding all users from deletion. You will need to re-run the command on $computer, if you wish to try again...`n" } elseif($DeleteUsers -like "/id:*") { & "C:DelProf2.exe" /c:$computer $DeleteUsers } else { Write-Host "`nImproper value entered, excluding all users from deletion. You will need to re-run the command on $computer, if you wish to try again...`n" } } }#End DelProf
Now, during the Read-Host I can enter a single user i.e. /id:user* , whatever it is, and it works fine. But, I want to be able to remove multiple users at once per the list I’m given. How would I go about sending multiple parameters to the delprof.exe command as if I were typing it directly into the command line? It gives me issues if I try something like /id:user* /id:Foo* but works perfect with the single parameter.
FYI, this forces the user of the function DelProf to implicitly include User Profiles to delete, instead of allowing mishandled entries and mistakes to pass through and delete things that weren’t meant to be.
I need this to scale also, so it could be two parameters or however many more I specify for that particular action.
I’m thinking maybe splitting the values of $DeleteUsers but, unsure if this is the proper direction:
function DelProf { param( [parameter(mandatory=$true)] [string[]]$computername ) foreach($computer in $computername) { & "C:DelProf2.exe" /l /c:$computer $DeleteUsers = Read-Host -Prompt "To delete User Profiles, please use the following syntax ; Wildcards (*) are accepted. `nExample: /id:user1 /id:smith* /id:*john*`n `nEnter proper syntax to remove specific users" $DeleteMe = $DeleteUsers.Trim("") foreach ($i in $DeleteMe) { if($DeleteUsers -like "/id:*") { & "C:DelProf2.exe" "/c:$computer", "$i" } Else {Write-Host "Do nothing"} } } }
Also, I don’t want to just loop through each entry and re-confirm each time, which is exactly what is going to happen if I leave it with the foreach($i in $DeleteMe) portion, I’d rather it feed all of my desired entries to the C:DelProf2 command initially and kill them all at once.
The .exe accepts something like this as a direct command:
.DelProf2.exe /c:Computer1 /id:TestUser1 /id:TestUser2 /id:NewGuy*
P.S. I’m doing this in a function so people aren’t messing things up by just using it straight off of the command-line… catering to the masses, I know.
Edit: provided the “error handling” portions of the function in the first example for whitespace, empties and $nulls.
submitted by /u/JBear_Alpha
[link] [comments]
The post Remove Account Unknown v2 appeared first on How to Code .NET.