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.
mikarin
Posts: 6
Joined: Sat Jan 25, 2020 5:51 am

SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin » Sat Jan 25, 2020 5:57 am

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 5 times
Last edited by mikarin on Sat Jan 25, 2020 8:32 am, edited 1 time in total.

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 8:29 am

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: 6
Joined: Sat Jan 25, 2020 5:51 am

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin » Sat Jan 25, 2020 8:33 am

okey. I added a attachment :)

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 8:36 am

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: 7214
Joined: Tue May 29, 2007 4:43 pm

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by Alexander Riedel » Sat Jan 25, 2020 9:00 am

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: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 9:32 am

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: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 9:35 am

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 4 times

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 9:52 am

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: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by jvierra » Sat Jan 25, 2020 10:05 am

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 8 times

mikarin
Posts: 6
Joined: Sat Jan 25, 2020 5:51 am

Re: SharePoint Commands Run itself bu Not with Powershell Studio

Post by mikarin » Sun Jan 26, 2020 3:40 am

So in which part of the script i should assign variables which can be accessible outside of script bloks?

Locked