Strange powershell script behavior, The term 'param' is not recognized as the name of cmdlet

wert mom 21 Reputation points
2021-11-09T14:49:22.993+00:00

I have a script to add an AD group.
For a while, it worked out correctly, and then stopped working with strange errors
param($name, $path, $groupScope)

$username = 'tst\ad-user'  
$encryptedStringPwd = Get-Content C:\Scripts\pwd\DNSpwd.txt  
$password = ConvertTo-SecureString $encryptedStringPwd  
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password   
  
Invoke-Command -ComputerName . -ScriptBlock {  
    New-ADGroup `  
        -Name $Using:name `  
        -SamAccountName $Using:name `  
        -GroupScope $Using:groupScope`  
        -Path $Using:path `  
        -GroupCategory Security  
} -authentication credssp -credential $cred  

the first error that appeared is147834-image.png

after that i took my code and copied to the newly created file and it worked 147846-image.png

after that I deleted the original non-working script and created the same one, the script did not work at first, after a couple of starts, it worked, but at the same time continued to write an error 147835-image.png
can someone help explain what it is and why it works like that

I will repeat once again that before that everything worked correctly and I am sure that no changes were made to the script

thanks!

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,858 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-11-10T15:23:18.083+00:00

    It's a shame you deleted the file that had the error. VS Code has an extension (Hex Editor) that's useful for things like this. The script is small enough to open in two editing panels with that extension and have a look at both of them side-by-side.

    VS Code also has a file comparison that would highlight any differences. Sometimes a weird, non-visible, character winds up in a file (e.g. thru miskeying a multi-character VS Code command).

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-11-09T15:55:51.777+00:00

    When you encounter that error, how has the editor color-coded the "param" at the beginning of the code? It you look at the (purportedly) different code in the file "test1.ps1", is if a different color to the "param" in the "AddADGroup.ps1" script?

    Copying the code you posted into VS Code doesn't produce an errors. But I use a different theme to the one you use.

    I don't see why you're getting that error in either script -- unless you're using PowerShell version 2.0!