从powershell执行powershell脚本时出错



我在执行以下代码时收到以下错误代码:

$localScriptPath= "C:AshishGpowershellscript12.ps1"
$ëncodedResonse = "77u/V3JpdGUtSG9zdCAnc2NyaXB0IGlzIGV4ZWN1dGVkIHN1Y2Nlc3NmdWxseScNCg0KcmV0dXJuICdUaGlzIGlzIHNjcmlwdCBvdXRwdXQn"
Write-Output "ëncodedResonse used is $ëncodedResonse"
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($ëncodedResonse)) | Out-File $localScriptPath -Force
$Content2 = get-content $localScriptPath
Write-Host "DECODED: " $Content2
$output =Invoke-Expression "& `"$localScriptPath`" "
Write-Host "scriptPath" used is $output

代码说明:

  1. 将脚本的路径分配到一个变量中
  2. 将编码字符串分配给变量
  3. 对字符串进行解码并复制到文件中
  4. 正在执行PowerShell脚本并获取错误

错误:

Write-Host : The term 'Write-Host' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At C:AshishGpowershellscript12.ps1:1 char:1
+ Write-Host 'script is executed successfully'
+ ~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

请看以下几点。

  1. 我在执行powerscript(C:\AshishG\powershell\script12.ps1(时出错
  2. 这个字符串是一个编码的格式,我可以解码它,我看到,解码的字符串也很好
  3. 我观察到,script12.ps1是以UTF-16 LE BOM格式生成的。这是在制造问题吗

我已经写了完整的代码,因为我不确定问题出在哪里?

错误来自您创建的.ps1文件,很可能来自编码的字符串。

创建.ps1文件后执行以下操作可以证明"Write-host..."前面有一个非打印字符:

PS C:>>[int[]][Char[]]((gc $localScriptPath)[0])[0]
65279
PS C:>

&65279是Unicode字符"ZERO WIDTH NO-BREAK SPACE"(U+FEFF(PowerShell正在用Write-HOst填充它,以创建一个无法识别的命令。

最新更新