随机字母显示在ppowershell脚本错误,使它很难找到错误源于何处

mbzjlibv  于 2023-03-03  发布在  Shell
关注(0)|答案(1)|浏览(116)

我不断收到随机字母powershell脚本错误。我做了一个改变鼠标方案脚本的基础上系统操作系统的主题。我不断收到随机字母,如下图所示,当运行powershell脚本:

这是我的剧本:

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
}

我试着删除脚本的一些部分,我认为问题源于注册表编辑。但没有运气。错误一直显示,无论我删除什么。
我试着把$PSStyle. OutputRendering ='PlainText '放在我的脚本的顶部,我收到了同样的错误。我试着在我的脚本中将NO_COLOR设置为1。没有修复这个问题。我没有访问运行脚本的应用程序的权限,我只能编辑我的脚本。顺便说一句,当软件开始使用PowerShell 7而不是Windows PowerShell(V5)时,我开始收到此错误。

dgsult0t

dgsult0t1#

注:以下内容适用于**PowerShell (Core) v7.2+
您所看到的是
ANSI / VT(虚拟终端)* 转义序列***,它们通常用于使 * 终端 * 呈现带有格式 * 的文本 ,特别是 * 彩色

如果您 * 逐字 * 呈现包含此类转义序列的字符串(例如通过消息框),您将得到乱码文本,如屏幕截图中所示,因此您需要指示PowerShell not 使用这些转义序列

因为看起来你是通过pwsh.exe,Windows PowerShell CLI,从 * 外部 * 调用PowerShell脚本,这是一个指示PowerShell * 不要 * 使用这些转义序列的简单方法,它在 * 调用pwsh.exe之前定义NO_COLOR环境变量(使用 * 任意 * 值,但1在概念上是有意义的)。[1]
另一种方法是执行$PSStyle.OutputRendering='PlainText'

  • 或者:作为传递给pwsh.exe-Command-c)参数的一部分
  • 或者:通过将此调用放在脚本的 * 顶部 *。

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

问题的证明

# 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 '1 / 0')
)

结果-注意乱码文本:

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

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

结果-不再出现乱码文本:

[1]如果您为 * 调用 * 进程定义它,pwsh子进程将 * 继承 * 它。与$PSStyle.OutputRendering设置不同,NO_COLOR在创建pwsh进程时必须已经存在才能有效。在从 Unix shell调用的情况下,您可以以 * 命令作用域 * 方式定义环境变量。例如NO_COLOR=1 pwsh -c '1 / 0'

相关问题