Studio Issue or Code?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

I think the above errors were a result of me replacing the file he sent. I copy and pasted into he existing after backing up and got the below. The only errors are the modifications that Alcha212 has made for the jobs.
  1. ERROR: You cannot call a method on a null-valued expression.
  2. MainForm.psf (190, 4): ERROR: At Line: 190 char: 4
  3. ERROR: +                 $timerJobTracker.Start()
  4. ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~
  5. ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  6. ERROR:     + FullyQualifiedErrorId : InvokeMethodOnNull
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

Is this related to the modification that were made using the jobs?

https://www.sapien.com/blog/2012/05/16/ ... ive-forms/
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

I am going to rebuild with some error control and look into adding the job functions in. Not sure how long it will take me but I would appreciate it if you guys look glance over my code when completed to offer any suggestions or if I even did it properly. Will write back up here when completed.

While I am working on that do either of you have a suggestion on how I can add a proper progress bar in there with the reduction of object processed and ensure it does not bug out when a new group of objects is added? As you can see in my code I go a bit backwards with my for statement.
Alcha212
Posts: 9
Last visit: Fri Jan 25, 2019 6:52 pm

Re: Studio Issue or Code?

Post by Alcha212 »

branhama wrote:After replacing the file I get the below errors in the console: I have just been running from the designer, but I did export to EXE and try with the same result. It seems all actions are completing properly.
  1. ERROR: Exception calling "AppendText" with "1" argument(s): "Object reference not set to an instance of an object."
  2. MainForm.psf (98, 3): ERROR: At Line: 98 char: 3
  3. ERROR: +             $tb_Activity.AppendText($Output)
  4. ERROR: +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  6. ERROR:     + FullyQualifiedErrorId : NullReferenceException
  7. ERROR:
  8. ERROR: Exception calling "AppendText" with "1" argument(s): "Object reference not set to an instance of an object."
  9. MainForm.psf (98, 3): ERROR: At Line: 98 char: 3
  10. ERROR: +             $tb_Activity.AppendText($Output)
  11. ERROR: +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  13. ERROR:     + FullyQualifiedErrorId : NullReferenceException
  14. ERROR:
Sorry about that, I forgot to account for the update script being run in the background and didn't actually get to test the script. I'd modify it and fix it but it seems you're taking a new direction.

In regards to the blog post you linked for responsive forms, that is indeed where I got most of the code. It's a helpful blog article for learning how to use jobs to prevent forms freezing up.
branhama wrote:I am going to rebuild with some error control and look into adding the job functions in. Not sure how long it will take me but I would appreciate it if you guys look glance over my code when completed to offer any suggestions or if I even did it properly. Will write back up here when completed.

While I am working on that do either of you have a suggestion on how I can add a proper progress bar in there with the reduction of object processed and ensure it does not bug out when a new group of objects is added? As you can see in my code I go a bit backwards with my for statement.
As for this, there is a control set that will help you out with this if you'd like to look into it. Here's a screenshot to show you where to find it:
Progress_Bar_Control_Set.png
Progress_Bar_Control_Set.png (22.05 KiB) Viewed 4354 times
Alcha212
Posts: 9
Last visit: Fri Jan 25, 2019 6:52 pm

Re: Studio Issue or Code?

Post by Alcha212 »

To also help you get started, here are two entries on info.sapien.com that cover the Progress Bar:

Spotlight Article: http://info.sapien.com/index.php/guis/g ... 9ncmVzcyJd

Creating Responsive Loops: http://info.sapien.com/index.php/guis/g ... 9ncmVzcyJd
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Studio Issue or Code?

Post by jvierra »

I can see an umber of errors in your code. You are never handling errors. In many cases you are just using ErrorAction to turn off error reporting. For TAKEWON you are just ignoring the errors.

One big design flaw is that altering ownership and removing ALL rules on a deep hierarchy can freeze depending on the current state of the security settings. If there are any links in the folder group then you could get it to a circular resolution black hoe if you don't skip the links.

My first question would be - why are you trying to do this? It appears that you are trying to fix something. IF you have a corrupt folder structure then this wil not work and will likely cause even more damage as you are not attempting to set any inheritance or propagation strategy. This might appear to work on folders that are not damaged or where the folders are small and not deep. Large folder hierarchy will likely fail and may well just cause you to freeze on a single call.

There is a utility that will fix the permissions. It is simple and runs very fast. Actually there are two utilities. One is SUBINACL and the other is FIXACL. YOU might want to look into using those tools.

In most cases yuo can correctly set and repair a folder hierarchy by setting and propagating the root in one call. It can still take a long time in deep folder sets.

I would suggest redesigning this to run as a job at a command prompt. The logging to the screen can be accomplished by outputting messages to the console which can be used to log to the screen when run from a form.

When setting ownership and permissions in a hierarchy we do not do this to all contained files and folder explicitly. We would just set the root folder and propagate it.

Yu also have to be careful with the current inheritance settings of the root folder as it can be in conflict with setting new permissions. You need to decide how you want to handle this issue or it can cause you to freeze when trying to fix large folder sets.

The Windows permissions wizard does all of this for you and has a nice GUI. It also guards against most mistakes and will not process links. Even the wizard can have issues if the underlying folders have problems although Microsoft has invested years in this utility and most of the issues have been addressed in the newer OS versions. In W2K the wizard would often freeze or totally destroy the ACLs if there was an issue. Don't try to re-invent the wizard. You will likely have to go through a big learning curve before you have a safe tool.

Look into ICACLS as another mostly safe tool to set a set of permissions on a hierarchy. ICACLS has also been deigned to prevent most issues with changing permissions on large folders sets. It is also much faster then using the Net Framework and can set the owner and will not set an incorrect ACL order or process links.

You should also set the initial owner on TAKEOWN to the admin group for safety.

Objects created by the system or Trusted Installer may not be easily modified. Some cannot be changed without booting into a repair prompt.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Studio Issue or Code?

Post by jvierra »

Found it:

https://gallery.technet.microsoft.com/S ... r-ff4db177

Here is a utility written in PowerShell that can explicitly set ownership on a file or folder recursively if needed. When we have an object this allows us to trap for errors and to also set the owner as opposed to just taking ownership.

In your case I would add the C# code to the global scope and also add the account with full control to the object. This script can be modified very easily to do this. It also allows us to avoid throwing away the errors.

When recursively taking ownership TAKSOWN is likely faster and safer as long as you capture and react to any errors before continuing to try and modify ACLs.

$results = TAKEOWN /F <object> /A /R
$LASTEXITCODE # check for non-zero
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

To the both of you thanks a ton for the help. My main reason for making this is work. We have network shares that were mismanaged in the past so there are tons of folder which admins no longer have access to that we need to repair. These folders could go 10+ levels in. The main problem that I have had so far with all tools I have used it that they seem to perform like a Get-ChildItem, which will ONLY get those objects you can see at the time, it will repair those and stop. Then you are forced to execute the same process over and over again until you have drilled through all the levels. Just one round of this can take 5+ hours to perform so I wanted something that would work until the job is done.

@jvierra I will check out that script and see what it is capable of however so far I have found none that actually perform the way I am needing it to. If anything it may help in adding proper error control to mine.

@Alcha212 Thanks for showing me that code for the jobs, I have noticed that some of my GUI apps seem to lockup while they are performing their actions. Hopefully this will get me in the right track to fix that and have better responding tools.
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

OK, working on the ProgressBar - Responsive Loop and have run into a couple snags I am having issues with.

Attached is my script export and also the folder structure I am using. Just some folders and blank txt documents.

My main issue right now seems to be the for loop. If I change the max to a high number is will cover all the folders/files. However if I attempt to set this to the number is should be it stops short even thought it doesn't seem to hit the max number.

And I am aware that my error control should be much better, just trying to work on the bugs before I complete everything.
Attachments
two.zip
(11.38 KiB) Downloaded 147 times
Repair-FolderPermissions.Export.ps1
(17.44 KiB) Downloaded 145 times
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Studio Issue or Code?

Post by branhama »

Uh, disregard the above post. I found the issue in my for statement. Will just have to figure out how to change it up so the progress bar work properly. Thanks to you both for the help!
This topic is 7 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked