SharePoint Commands Run itself bu Not with Powershell Studio

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 4 years and 2 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
mikarin
Posts: 10
Last visit: Thu Oct 22, 2020 6:18 am

SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin »

Hello

I have problem about script which runs on Sharepoint environment. When i just run the cmdlet itself, (merge-splogfile) creating a file and its visible on desktop. When i run whole script via PowerShell Studio through button, process is stucking at about %90 percent and not completing itself.

Other buttons are working fine
$path and $correlation values are okay but script is not finishing.

Any idea?

attachment added.
Attachments
correlation.Export.ps1
(7.85 KiB) Downloaded 74 times
Last edited by mikarin on Sat Jan 25, 2020 7:32 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

When posting a large amout of code please post the code as an attachment/ YOu can attach a PSF file to the post.
mikarin
Posts: 10
Last visit: Thu Oct 22, 2020 6:18 am

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin »

okey. I added a attachment :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

Please add the PSF file as an attachment. You have added the exported PS1 file. The PSF file is easier to load and gives more information.
User avatar
Alexander Riedel
Posts: 8488
Last visit: Mon Apr 15, 2024 3:28 pm
Answers: 20
Been upvoted: 37 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by Alexander Riedel »

Permissions or threading are the usual suspects. Try running it with STA. Does it require elevation? When you run the script from PowerShell Studio, it will not execute any profile.
So if your profile has specific presets you should transfer those to your script. This way you never create local dependencies.
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: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

I do not believe this is due to the threading model as the code itself is not written correctly and the log is being generated for all log entries due to the code design error.
The reason I asked for the PSF is so I could give the attached as an answer:

Unfortunately the site no longer allows us to upload files. Something seems to have disabled that capability.

The issue is one of scoping. I also showed the better method for including the VB assembly and using it. Ideally we would want to use a textbox on the form for the input.

Here is the changed code. Add a textbox as named to save the value.

Code: Select all

$path = [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$form1_Load={
}

$buttonFilename_Click={
    $text = [Microsoft.VisualBasic.Interaction]::InputBox('Enter the File Name:', 'Path')	
}

$buttonCorrelationID_Click={
    $textboxCorrelationID.Text = [Microsoft.VisualBasic.Interaction]::InputBox('Correlation', 'Enter the CorrelationID:')
}

$buttonMerge_Click={
	$result= Merge-SpLogFile -path C:\Users\$env:USERNAME\Desktop\$text.txt -Correlation $textboxCorrelationID.Text
}

$button1_Click={
	Invoke-Item -path C:\Users\$env:USERNAME\Desktop\$text.txt
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

Nope - wrong. The editor is not available under Edge (Chrome version) but is with IE.

Here is the PSF.

Edge has the "options" tab but no "attachments" tab. Strange.
Attachments
correlation.psf
(31.63 KiB) Downloaded 86 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

To be more specific, when using variables in events the variables are locally scoped to the event and not visible in other events or outside of the event. The best way to access values is to use controls as the controls are scoped to the script and available anywhere inside the form and all events.

Another approach is to declare the variables as "script" scope and then it will be available in events.

Code: Select all

$buttonCorrelationID_Click={
    $script:text = [Microsoft.VisualBasic.Interaction]::InputBox('Correlation', 'Enter the CorrelationID:')
}
This will then be available in the event that needs the variable. Accessing the variable can be done with or without the scope modifier but the scope modifier must be used anywhere the variable is assigned.

You can also pre-define the variable outside of any event code and it will be scoped at the script scope but assignments will still always require the scope modifier.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra »

PS> If the issue was threading I believe we should get an exception and the event would complete. It might depend on which version of the server support module is installed. For 2016 and later it should just run until all log entries are merged which can take a very long time on large servers. I am surprised that a null CorrelationID causes this behavior. I would have thought it would cause an error.

The event code looks like this:

Code: Select all

Add-Type -AssemblyName Microsoft.VisualBasic

$buttonFilename_Click={
    $script:filename = [Microsoft.VisualBasic.Interaction]::InputBox('Enter the File Name:', 'Path')	
}

$buttonCorrelationID_Click={
    $textboxCorrelationID = [Microsoft.VisualBasic.Interaction]::InputBox('Correlation', 'Enter the CorrelationID:')
}

$buttonMerge_Click={
	$result= Merge-SpLogFile -path "C:\Users\$env:USERNAME\Desktop\$filename.txt" -Correlation $textboxCorrelationID.Text
}

$button1_Click={
	Invoke-Item "C:\Users\$env:USERNAME\Desktop\$filename.txt"
}
The following PSF version demonstrates both methods and shows how to correctly incorporate the variables into the strings.
Attachments
correlation2.psf
(31.63 KiB) Downloaded 93 times
mikarin
Posts: 10
Last visit: Thu Oct 22, 2020 6:18 am

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin »

So in which part of the script i should assign variables which can be accessible outside of script bloks?
This topic is 4 years and 2 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