Function not running within Form

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 5 years and 7 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.
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Function not running within Form

Post by mqh77777 »

PowerShell Studio 2018 v5.5.153
OS: Windows 10 x64

I have a PowerShell Studio form that is compiled into an .EXE. I wanted to add a write-log function so I can log certain things when they are clicked on. This function works fine with its run from or called by .ps1 files. But when I place this inside of my form it never writes any output.

Code: Select all

function Write-Log
{
  <#
   .Synopsis
    Writes to a log file.

   .Description
    This function writes to a log file $LogPath

   .Parameter logline
  #>
	param (
		[Parameter(Mandatory = $true)]
		[String]$LogLine
	)
	# define default path to Powershell log files
	$LogPath = (Get-ChildItem env:Windir).value + "\logs\PowerShell"  
		
	# if the default Log folder does not exist, create it
	if (-not (test-path $LogPath))
	{
		New-Item -ItemType directory -Path $LogPath
	}
	# Generate the log file name by using the current Script's name. Add a Date/Time stamp to each entry.
	Add-Content -Path ($LogPath + "\" + (split-path $MyInvocation.PSCommandPath -Leaf).split(".")[0] + ".log") -Value ((get-date -format g) + "  " + $LogLine) -Force
}

Write-Log ("Lets begin shall we")
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Function not running within Form

Post by mxtrinidad »

First, have you tested the function you just pasted here?
It has an error: "(Get-ChildItem env:Windir)"!!

Should use $env:Windir, and not env:Windir.

:)
User avatar
mqh77777
Posts: 252
Last visit: Mon Feb 26, 2024 10:07 am
Has voted: 1 time

Re: Function not running within Form

Post by mqh77777 »

There are no errors in this script, run it, it works perfect. If you run this by itself it returns C:\Windows (Get-ChildItem env:Windir).value
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Function not running within Form

Post by mxtrinidad »

Interesting!

I never use the Get-ChildItem to build a path, as the main purpose is to list the content of a folder. Normally, using $env:Windir + "\myfolder" should be simple enough.

PS [162] > $env:Windir+"\MyFolder"
C:\WINDOWS\MyFolder

Still, can you share the form? This way it will show how are you using the function you created.
:)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Function not running within Form

Post by davidc »

To write into the Windows folder, you need administrative writes, so verify that your executable has the necessary permissions.

In addition, $MyInvocation.PSCommandPath will not work in the packager host. Use the following to get the script name:

Code: Select all

function Get-ScriptName
{
<#
	.SYNOPSIS
		Get-ScriptName returns the name of the script.

	.OUTPUTS
		System.String
	
	.NOTES
		Returns the correct name within a packaged executable.
#>
	[OutputType([string])]
	param ()
	if ($null -ne $hostinvocation)
	{
		$hostinvocation.MyCommand.Name
	}
	else
	{
		 $script:MyInvocation.MyCommand.Name
	}
}
David
SAPIEN Technologies, Inc.
This topic is 5 years and 7 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.