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!

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 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 47,901 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!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.