Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell 5.1 class definition method declaration

thobart

Programmer
Aug 15, 2024
1
0
1
I'm defining my first custom class in powershell 5.1. I'm creating methods for my class. MS Learn tells me I must have a return type in the method definition but it constantly generates an error in the IDE

# Define a class
class MyError
{
[string] $date
[ErrorRecord] $PSErrorObject
[string] $eMail
[string] $Script
[string] $scriptFolder
[int] $LineNumber
[string] $PSErrorMessage
[ValidateSet("Administrator","User")] [string] $Privilege
[string] $ScriptVersion
[string] $LogFile
[string] $LogFileFolder
[string] $WorkstationName
[string] $UserName

# Process the error. Main control method. Calls the appropriate methods. Called when an
# error is caught.
[string] Process() {
return "foobar"
}

}

The '[string] ' Process() line is red underlined saying: Missing closing ']' in Statement block or type definition
If I remove the [string] it's fine but violates what MS says is required for a method in a class definition. Also, if I just copy & paste their example (which looks the same) it's also happy.
# Method
[string] GetFullName() {
return "$($this.FirstName) $($this.LastName)"
}

I'm most confused.
 
this is because of Process is reserved word, rename it and it will work
 

Part and Inventory Search

Sponsor

Back
Top