Powershell command not work when compiled

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 1 year and 1 month 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.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Powershell command not work when compiled

Post by Eanna_bearn »

Hello,

I made a tool which need RSAT AD on computer.
I try to install it if not present and do a loop to wait if it is installed before continue startup.pss with another form.

In standard user when i launch this command in Powershell ISE RSAT got installed :

Code: Select all

		$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
		Start-Process -FilePath "powershell" -Credential $cred -ArgumentList $args

but when i put it in my script in powershell studio nothing happen and i don't manage to install it ... any idea ? :?


Code: Select all

$formPremierLancement_Load={

	$variableoutput_AD = invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { get-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0" | select-object -Expandproperty state }
	$cap_AD = $variableoutput_AD.value
	if (-not($cap_AD -eq "Installed"))
	{
		$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
		Start-Process -FilePath "powershell" -Credential $cred -ArgumentList $args
		$AD_installed = "nok"
		
	}
}

$formPremierLancement_Shown = {
	$erreur = "0"
	[System.Windows.Forms.Application]::DoEvents()
	
	sleep -s 40
	

	$counter = 0
	$variableoutput_AD = invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { get-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0" | select-object -Expandproperty state }
	$cap_AD = $variableoutput_AD.value
	if ($cap_AD -eq "Installed")
	{
		$AD_installed = "ok"
	}

	while (-not ($AD_installed -eq "ok"))
	{
		[System.Windows.Forms.Application]::DoEvents()
		$variableoutput_AD = invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { get-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0" | select-object -Expandproperty state }
		$cap_AD = $variableoutput_AD.value
		if ($cap_AD -eq "Installed")
		{
			$AD_installed = "ok"
		}
		elseif ($counter -eq 6)
		{
			$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
			Start-Process -FilePath "powershell" -Credential $cred -ArgumentList $args
		}
		elseif ($counter -eq 18)
		{
			$script:MyReturnValue = "1"
			break
		}
		Start-Sleep -s 5
		$counter++
	}
	
	
	
	$formPremierLancement.Close()
}

if i put all my loop in powershell_ise in standard user without the forms, rsat ad is installed correctly i can follow with write-output in console

Code: Select all

$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
			Start-Process -FilePath "powershell" -Credential $cred -ArgumentList $args	
			sleep -s 40
	$variableoutput_AD = invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { get-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0" | select-object -Expandproperty state }
	$cap_AD = $variableoutput_AD.value
	if ($cap_AD -eq "Installed")
	{
		$AD_installed = "ok"
		write-host "installed youhou !!!"
	}

	while (-not ($AD_installed -eq "ok"))
	{
		[System.Windows.Forms.Application]::DoEvents()
		$variableoutput_AD = invoke-command -ComputerName localhost -Credential $cred -ScriptBlock { get-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0" | select-object -Expandproperty state }
		$cap_AD = $variableoutput_AD.value
		if ($cap_AD -eq "Installed")
		{
			$AD_installed = "ok"
			write-host "installed youhou !!!"
		}
		elseif ($counter -eq 6)
		{
			$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
			Start-Process -FilePath "powershell" -Credential $cred -ArgumentList $args
			write-host "not installed lauch command again"
		}
		elseif ($counter -eq 18)
		{
			write-host "pas installé don t work"
			break
		}
		Start-Sleep -s 5
		$counter++
	}
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Powershell command not work when compiled

Post by Eanna_bearn »

anyone to help me please ? :(
Really do not understand what is not working ...
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: Powershell command not work when compiled

Post by brittneyr »

Is it not working when you run your project (F5) or is it not working when you package it as an executable? You say 'compiled' so I just need to clarify.

It is ever reaching the code to install? Have you tried putting a breakpoint on the line to see?
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Powershell command not work when compiled

Post by Eanna_bearn »

it is not working when i package it as an executable.

i have install powershell studio on the AD server in my lab test, and try to launch the exe on client side so i can not see what happen on client i just launch the .exe file.

When i launch all commands in powershell_ise on client everything is ok.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: Powershell command not work when compiled

Post by brittneyr »

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:
Operating system:
PowerShell version(s):

*** Please add details and screenshots as needed below. ***
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Powershell command not work when compiled

Post by Eanna_bearn »

On the AD server where Powershell studio is installed

Product: PowerShell Studio 2023 (64 Bit)
Build: v5.8.217
OS: Windows Server 2016 Standard (64 Bit)
Build: v10.0.14393.0

On the "client" computer where i want to use the tool i made : Windows 10 21h2

I use Powershell V5 Windows forms
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: Powershell command not work when compiled

Post by brittneyr »

Please answer the following:

-Does your form project run (F5) in PowerShell Studio without any errors?
-Are there any errors when you run your executable? For example, are there any popups when you run or errors reported to Event Viewer?
-Are you sure you don't need administrator rights on the "client computer"?
-Have you tested just running your project exported as a script (not executable) on the 'client' machine to verify that it runs? If not, please try.
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Powershell command not work when compiled

Post by Eanna_bearn »

yes on the server it run fine
No popup or messages on event viewer
yes i need administrator rights to launch install rsat but it is why i use :

Code: Select all

$domain = (Get-WmiObject -Class Win32_ComputerSystem).Domain

$username = "$domain\adminusername"
$password = ConvertTo-SecureString "Passwordofadminusername" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
i just try to export it, error i have is "error in file path with start process"

Code: Select all

$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
		Start-Process -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Credential $cred -ArgumentList $args
but when i launch powershell_ise with standard user with no privilèges :

Code: Select all

$username = "$domain\adminusername"
$password = ConvertTo-SecureString "Passwordofadminusername" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
$args = '-noprofile -command "Add-WindowsCapability -online -Name "Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0"'
		Start-Process -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Credential $cred -ArgumentList $args
no problem at all, it run fine and install rsat :shock:
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: Powershell command not work when compiled

Post by brittneyr »

Can you please provide a screenshot of the error you are receiving when you run the exported script? Just to clarify, the "error in file path with start process" is when you run the exported script on the client computer? How are you running the exported script on the client machine?

The more details provided, the easier it will be to assist you.
Brittney
SAPIEN Technologies, Inc.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Powershell command not work when compiled

Post by Eanna_bearn »

Hello here is
Capture.JPG
Capture.JPG (72.04 KiB) Viewed 5498 times
The strange thing is if i launch commands with standard user rights in powershell_ise directly there is no error, all run fine.

When i export my projet in ps1 or if i put commands in .ps1 file directly i have this error.

This happen on client where i want to install RSAT AD plugin.

When i run ps1 files with admin rights i have no problem at all.


I do not understand the differences launching commands in powershell_ISE and commands in PS1 files in standard user because i use admin credentials in my script.
This topic is 1 year and 1 month 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.