PowerShell脚本中显示的随机字母使得很难找到错误的根源。

huangapple go评论62阅读模式
英文:

Random letters showing in ppowershell script error making it so hard to find where the error stems from

问题

我不确定你希望我翻译哪些部分,因为整个内容似乎都是关于一个 PowerShell 脚本错误的描述。你是否需要我为你翻译特定的部分或提供关于错误的解决方案?如果是,请具体说明需要翻译的部分。

英文:

I keep receiving random letters powershell script error.
I made a changing mouse scheme script based on system os theme. I keep receiving random letters as shown in the image below when running the powershell script:
PowerShell脚本中显示的随机字母使得很难找到错误的根源。

This is my script:

param (
    [Parameter(Mandatory=$true)][int]$daySegment2,  # 0 = Day, 1 = Night
    [Parameter(Mandatory=$true)][int]$daySegment4,  # -1 = N/A, 0 = Sunrise, 1 = Day, 2 = Sunset, 3 = Night
    [Parameter(Mandatory=$true)][bool]$nightMode,   # True if night mode is enabled
    [Parameter(Mandatory=$false)][string]$imagePath # Path to current wallpaper image
)

$Location = "~\AppData\Local\WinDynamicDesktop\scripts\globalScripts\NightValue.xml"
$PreviousNightValue = Import-CliXml $Location

$Location2 = "~\AppData\Local\WinDynamicDesktop\scripts\globalScripts\StartValue.xml"
$StartValue = Import-CliXml $Location2

$NightValue = If ($nightMode) {1} Else {$daySegment2}

if ( ($NightValue -eq 0 -and $StartValue -eq 1) -or ($NightValue -ne $PreviousNightValue -and $StartValue -eq 0 -and $NightValue -eq 0))
{
$RegConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]”CurrentUser”,”$env:COMPUTERNAME”)
$RegCursors = $RegConnect.OpenSubKey(“Control Panel\Cursors”,$true)
$RegCursors.SetValue(“”,”W11 Cursor Light HD v2.2 by Jepri Creations”)
$RegCursors.SetValue(“AppStarting”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\working.ani”)
$RegCursors.SetValue(“Arrow”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\pointer.cur”)
$RegCursors.SetValue(“Crosshair”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\precision.cur”)
$RegCursors.SetValue(“Hand”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\link.cur”)
$RegCursors.SetValue(“Help”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\help.cur”)
$RegCursors.SetValue(“IBeam”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\beam.cur”)
$RegCursors.SetValue(“No”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\unavailable.cur”)
$RegCursors.SetValue(“NWPen”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\handwriting.cur”)
$RegCursors.SetValue(“Person”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\person.cur”)
$RegCursors.SetValue(“Pin”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\pin.cur”)
$RegCursors.SetValue(“precisionhair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“SizeAll”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\move.cur”)
$RegCursors.SetValue(“SizeNESW”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\dgn2.cur”)
$RegCursors.SetValue(“SizeNS”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\vert.cur”)
$RegCursors.SetValue(“SizeNWSE”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\dgn1.cur”)
$RegCursors.SetValue(“SizeWE”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\horz.cur”)
$RegCursors.SetValue(“UpArrow”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\alternate.cur”)
$RegCursors.SetValue(“Wait”,”%SYSTEMROOT%\Cursors\W11_light_v2.2\busy.ani”)
$RegCursors.Close()
$RegConnect.Close()
$CSharpSig = @’
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
             uint uiAction,
             uint uiParam,
             uint pvParam,
             uint fWinIni);
‘@
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo –PassThru
$CursorRefresh::SystemParametersInfo(0x0057,0,$null,0)
}
ElseIf (($NightValue -eq 1 -and $StartValue -eq 1) -or ($NightValue -ne $PreviousNightValue -and $StartValue -eq 0 -and $NightValue -eq 1))
{
$RegConnect = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]”CurrentUser”,”$env:COMPUTERNAME”)
$RegCursors = $RegConnect.OpenSubKey(“Control Panel\Cursors”,$true)
$RegCursors.SetValue(“”,”W11 Cursors Dark HD v2.2 by Jepri Creations”)
$RegCursors.SetValue(“AppStarting”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\working.ani”)
$RegCursors.SetValue(“Arrow”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\pointer.cur”)
$RegCursors.SetValue(“Crosshair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“Hand”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\link.cur”)
$RegCursors.SetValue(“Help”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\help.cur”)
$RegCursors.SetValue(“IBeam”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\beam.cur”)
$RegCursors.SetValue(“No”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\unavailable.cur”)
$RegCursors.SetValue(“NWPen”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\handwriting.cur”)
$RegCursors.SetValue(“Person”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\person.cur”)
$RegCursors.SetValue(“Pin”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\pin.cur”)
$RegCursors.SetValue(“precisionhair”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\precision.cur”)
$RegCursors.SetValue(“SizeAll”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\move.cur”)
$RegCursors.SetValue(“SizeNESW”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\dgn2.cur”)
$RegCursors.SetValue(“SizeNS”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\vert.cur”)
$RegCursors.SetValue(“SizeNWSE”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\dgn1.cur”)
$RegCursors.SetValue(“SizeWE”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\horz.cur”)
$RegCursors.SetValue(“UpArrow”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\alternate.cur”)
$RegCursors.SetValue(“Wait”,”%SYSTEMROOT%\Cursors\W11_dark_v2.2\busy.ani”)
$RegCursors.Close()
$RegConnect.Close()
$CSharpSig = @’
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
                uint uiAction,
                uint uiParam,
                uint pvParam,
                uint fWinIni);
‘@
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo –PassThru
$CursorRefresh::SystemParametersInfo(0x0057,0,$null,0)
}

$StartValue = 0
if (($NightValue -ne $PreviousNightValue))
{
    $NightValue | Export-Clixml -path $Location
}

if ($StartValue -ne 0)
{
    $StartValue | Export-Clixml -path $Location2
}

I tried removing some parts of the script, I think the problem stems for registry editing. But had no luck. the error kept showing no matter what I delete.

I tried putting the
$PSStyle.OutputRendering='PlainText'
at the top of my script, I received the same error.
I tried setting the NO_COLOR to 1 inside my script. Did not fix the issue.
I don't have access to the application that runs the script, I just can edit my script.
BTW, that's started happening when the software started using PowerShell 7 instead of windows PowerShell(V5) I started getting this error.

答案1

得分: 3

以下是已翻译的内容:

注意:以下内容适用于 PowerShell (Core) 版本 7.2+

你所看到的是通常用于使终端呈现带有格式的文本,特别是带颜色的 ANSI / VT (虚拟终端) 转义序列

如果你将包含这些转义序列的字符串 原样 呈现,例如通过消息框,你会得到乱码文本,就像你的屏幕截图中所示,所以你需要告诉 PowerShell 不要使用这些转义序列

由于看起来你是从 外部 调用 PowerShell 脚本,通过 pwsh.exe,Windows PowerShell 命令行界面,一个简单的方法是在调用 pwsh.exe 之前定义 NO_COLOR 环境变量(使用任何值,但 1 是合理的概念).<sup>[1]</sup>

另一种方法是执行 $PSStyle.OutputRendering=&#39;PlainText&#39;

  • 要么:作为传递给 pwsh.exe-Command (-c) 参数的一部分

  • 要么:将这个调用放在你脚本的 顶部

有关更多信息,请参阅概念性的 about_ANSI_Terminals 帮助主题。


问题演示

# 调用 PowerShell (Core) 7.2+ CLI 并引发错误,
# 然后通过消息框呈现输出:
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c &#39;1 / 0&#39;)
)

结果 - 请注意乱码文本:

PowerShell脚本中显示的随机字母使得很难找到错误的根源。

如果你首先定义 NO_COLOR,问题就会消失:

$env:NO_COLOR=1
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c &#39;1 / 0&#39;)
)

结果 - 不再有乱码文本:

PowerShell脚本中显示的随机字母使得很难找到错误的根源。

<sup>[1] 如果你为 调用 进程定义它,pwsh 子进程将 继承 它。与 $PSStyle.OutputRendering 设置不同,NO_COLOR 必须在创建 pwsh 进程时已经存在才能生效。在从 Unix shell 调用的情况下,你可以以 命令范围的 方式定义环境变量,例如 NO_COLOR=1 pwsh -c &#39;1 / 0&#39;</sup>。

英文:

Note: The following applies to PowerShell (Core) v7.2+:

What you're seeing are ANSI / VT (Virtual Terminal) escape sequences that are typically used to make terminals render text with formatting, notably colored.

If you render strings containing such escape sequences verbatim, such as via a message box, you'll get garbled text as in your screenshot, so you'll need to instruct PowerShell not to use these escape sequences.

Since it looks like you're calling your PowerShell script from the outside, via the pwsh.exe, the Windows PowerShell CLI, a simple way to instruct PowerShell not to use these escape sequences it to define the NO_COLOR environment variable (with any value, but 1 makes conceptual sense) before calling pwsh.exe.<sup>[1]</sup>

An alternative is to execute $PSStyle.OutputRendering=&#39;PlainText&#39;:

  • Either: as part of a -Command (-c) argument passed to pwsh.exe

  • Or: By placing this call at the top of your script.

For more information, see the conceptual about_ANSI_Terminals help topic.


Demonstration of the problem:

# Call the PowerShell (Core) 7.2+ CLI and provoke an error,
# then render the output via a message box:
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c &#39;1 / 0&#39;)
)

Result - note the garbled text:

PowerShell脚本中显示的随机字母使得很难找到错误的根源。

The problem goes away if you define NO_COLOR first:

$env:NO_COLOR=1
(New-Object -ComObject WScript.Shell).Popup(
  (pwsh -c &#39;1 / 0&#39;)
)

Result - no more garbled text:

PowerShell脚本中显示的随机字母使得很难找到错误的根源。


<sup>[1] If you define it for the calling process, the pwsh child process will inherit it. Unlike the $PSStyle.OutputRendering setting, NO_COLOR must already exist when the pwsh process is created in order to be effective. In cases where you're calling from a Unix shell, you can define the environmant variable in a command-scoped way, e.g. NO_COLOR=1 pwsh -c &#39;1 / 0&#39;</sup>

huangapple
  • 本文由 发表于 2023年2月27日 02:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定