0

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)

$username = 'tst\user-ad'
$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 Global `
        -Path $Using:path `
        -GroupCategory Security
} -authentication credssp -credential $cred

the first error that appeared is first error

after that i took my code and copied to the newly created file and it worked work in another file

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 work/error

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

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

thanks!

bob stinger
  • 1,314
  • 1
  • 4
  • 4
  • 2
    Please insert all error messages and code samples directly into the post, formatted as code, like you did with your first code sample. Screenshots can be hard to read and are not machine-searchable, which makes it more difficult for other users to find similar questions. – zett42 Nov 09 '21 at 15:22
  • unfortunately, there is no way to insert the error code due to the fact that this is an isolated environment in which there is no Internet connection and the clipboard does not work – bob stinger Nov 10 '21 at 10:04

1 Answers1

1

The likeliest explanation is that your script file contains invisible control characters on the param(...) line, specifically right before, right after, or inside the word param.

Use the Format-Hex cmdlet to inspect your file for such characters and remove them.

Alternatively, use helper function Debug-String, available from this Gist, which makes it easier to spot unusual characters, as it prints regular characters as-is, while visualizing control characters as escape sequences, such as `u{200b} to identify the invisible ZERO WIDTH SPACE character, U+200B, for instance.

To demonstrate the problem using this particular invisible control character, with the help of Invoke-Expression (which should generally be avoided, however).

PS> Invoke-Expression ('{0}param($name, $path)' -f [char] 0x200b)

param: The term 'param' is not recognized as a name of a cmdlet, function, script file, ...

Note that you can't tell the presence of the zero-width space from the error message.

Without the invisible control character, the expression would result in no output (because it would execute successfully, and a parameter declaration block produces no output).

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thanks for your answer, unfortunately the file with the broken script was deleted, as it was on a production environment, at the moment the error cannot be reproduced, but if it reappears, I will use your advice. thanks – bob stinger Nov 10 '21 at 10:15