question

wertmom-6439 avatar image
0 Votes"
wertmom-6439 asked RichMatheisen-8856 answered

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


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-server-powershellwindows-active-directory
image.png (89.7 KiB)
image.png (119.8 KiB)
image.png (131.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered wertmom-6439 commented

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!

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


color encoding did not change when copying the script to another file.
my workaround was that I renamed the test.ps1 file to the name I needed, and deleted the non-working file, and after that everything worked for me, while I did not change the contents of the file
but why it works like that, I don’t understand, and I don’t want it to happen again

0 Votes 0 ·