script will not run without errors

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 months 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: script will not run without errors

Post by jvierra »

Per Dan and also build the script as a PS1 file and run until it works.

You do not need elevation to access AD if you are a Domain Admin.

You cannot close a form from the Load event. Place your code in the Activated event and it will work.

Why use a form?
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: script will not run without errors

Post by mqh77777 »

I have full rights in our environment. I am a local Admin, Domain Admin and Enterprise Admin. So rights should not be an issue. The form does nothing except try to execute the code I provided. So what am I doing wrong? How come this does not run? Is there a way to create a .EXE that runs without any user interaction using this tool?

Thank you.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: script will not run without errors

Post by jvierra »

Run this a a command prompt to test then create the event and use this scriptblock minus the "Invoke" line
  1. $formPostBuildADTasks_Activate = {
  2.     $root = $dom.GetDirectoryEntry()
  3.     $searcher = [adsisearcher]"cn=$env:computername"
  4.     $result = $searcher.FindOne()
  5.     $computer = [ADSI]$result.path
  6.     $computer.Description = (get-ItemProperty hklm:\SOFTWARE\MGH).Description
  7.     $computer.CommitChanges()
  8.    
  9.     $OU = (get-ItemProperty hklm:\SOFTWARE\Microsoft\MPSD\OSD).OSDDomainOUName
  10.     $targetOU = [ADSI]"LDAP://OU"
  11.     $computer.psbase.Moveto($targetOU)
  12. }
  13. $formPostBuildADTasks_Activate.Invoke()
Your use of the registry is odd and may be the source of many of you issues. YOU will need to debug this without a form.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: script will not run without errors

Post by dan.potter »

mqh777 wrote:I have full rights in our environment. I am a local Admin, Domain Admin and Enterprise Admin. So rights should not be an issue. The form does nothing except try to execute the code I provided. So what am I doing wrong? How come this does not run? Is there a way to create a .EXE that runs without any user interaction using this tool?

Thank you.
I'm just guessing registry as one of my pc's here prompts me for elevation, it's a computer policy and has nothing to do with domain permissions. start-run regedit..what's it do?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: script will not run without errors

Post by jvierra »

dan.potter wrote:
mqh777 wrote:I have full rights in our environment. I am a local Admin, Domain Admin and Enterprise Admin. So rights should not be an issue. The form does nothing except try to execute the code I provided. So what am I doing wrong? How come this does not run? Is there a way to create a .EXE that runs without any user interaction using this tool?

Thank you.
I'm just guessing registry as one of my pc's here prompts me for elevation, it's a computer policy and has nothing to do with domain permissions. start-run regedit..what's it do?
Registry keys are always readable.

The code has many errors and issues which can create errors. Without the error message there is no way to help.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: script will not run without errors

Post by mqh77777 »

I can open and update any part of the registry. I can create keys, edit keys, delete keys etc.... plus the script I am trying to run only reads registry keys (that do exist) and you don't need admin rights to read the registry.

Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: script will not run without errors

Post by jvierra »

I repeat - you are trying to run a script in the form load event that tries to close the form. That will not work as expected. Run my copy of your script and be sure it works then place it in the form activated event.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: script will not run without errors

Post by Alexander Riedel »

One thing that I also notice is that you try to use elevation AND RUNAS. That can not be done in one step. The way Microsoft implemented that prohibits that.
If you want to run a application as an elevated admin and you are a normal user you need to use RunAs first and THEN elevate.

But since the elevation is determined by a manifest BEFORE and exe is even loaded, Windows will do that backwards.

So in order to go from normal user to elevated admin you need a starter exe. The starter exe uses runas with the desired credentials as a member of the admin group.
Then you use elevation on the actual application to get to where you need to be.
Depending on your system settings there is an elevation prompt. That is a Windows functionality.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: script will not run without errors

Post by jvierra »

But the script does not require elevation. It is simple ADSI calls that only require the user to be a domain admin. The problem is the code and how it is deployed.

The info about elevation is good when elevation is required.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: script will not run without errors

Post by Alexander Riedel »

I mentioned it because the OP wrote:

"Under Build Settings I have chosen the following.
Embed a default manifest for elevation
Alternate Credentials" I've used a domain admin account and password. "
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 7 years and 6 months 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