在今天的一些测试中,我遇到了一个意想不到的问题,我不明白为什么会这样。下面是我用来复制问题的代码。它只是较大项目的一小部分。
如果有帮助,正在 Windows 10 Build 1709 上进行测试
PS1 文件和 BAT 文件的名称相同。
导致错误的方法
- 通过运行 PS1 文件
Right-Click - Run with PowerShell会导致错误 PowerShell ISE以非管理员模式打开,然后打开/运行脚本将导致错误- 以管理员或非管理员身份运行 BAT 文件将导致错误
避免错误的方法
PowerShell ISE以管理员模式打开,然后打开/运行脚本不会导致错误Script:在最后2行代码的变量前面加上不管脚本怎么执行都不会报错- 使用 VSCode,它将如下所示工作。在集成终端中运行它,它会看到它没有作为 运行
Admin,它将PowerShell.exe在 VSCode 之外启动并且没有问题地工作
-
为什么我Script:的函数前面有变量?这是我可以在函数中设置变量以在函数外使用的唯一方法。这篇文章中未列出的其他 25 个左右的变量没有问题,但是,它们在设置后不会像这两个一样被修改。
问题
- 为什么,如果在
Admin模式下运行 ISE,它会工作? - 如果它以管理员身份重新启动,为什么它不起作用?
- 为什么 VSCode 不关心并且无论如何它都可以工作?
有些东西没有意义,我无法确定它。
这是错误
无法覆盖变量 NetFX3,因为该变量已被优化。尝试使用 New-Variable 或 Set-Variable cmdlet(不带任何别名),或点源您用于设置变量的命令。在 C:\Users\a502690530\Desktop\Testing2.ps1:14 char:5 + [string]$Script:NetFX3 = $BAT_Files_Path + "NetFX3.zip" + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (NetFX3:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritableRare
无法覆盖变量 Power_Plan,因为该变量已被优化。尝试使用 New-Variable 或 Set-Variable cmdlet(不带任何别名),或点源您用于设置变量的命令。在 C:\Users\a502690530\Desktop\Testing2.ps1:15 char:5 + [string]$Script:Power_Plan = $BAT_Files_Path + "Power_Plan.zip" + ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo : WriteError: (Power_Plan:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritableRare
这是代码
# Checks if running as an administrator. If not, it will relaunch as an administrator
If (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$Arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
Start-Process Powershell -Verb RunAs -ArgumentList $Arguments
Break
}
[string]$ErrorActionPreference = "Continue"
[string]$BAT_Files = $Root_Path + "BAT_Files\"
Function Set-FilePaths ([string]$BAT_Files_Path) {
# BAT Files Paths (ZIPs only!!!)
[string]$Script:NetFX3 = $BAT_Files_Path + "NetFX3.zip"
[string]$Script:Power_Plan = $BAT_Files_Path + "Power_Plan.zip"
Set-Lists
}
function Set-Lists {
# List of BAT Files (ZIPs)
[System.Collections.ArrayList]$Script:List_Of_BAT_Files = @(
$NetFX3
$Power_Plan
)
}
Set-FilePaths `
-BAT_Files_Path $BAT_Files
PAUSE
$NetFX3 = ((Split-Path $NetFX3 -Parent) + "\NetFX3\")
$Power_Plan = ((Split-Path $Power_Plan -Parent) + "\Power_Plan\")
BAT 文件启动
REG ADD "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /T REG_SZ /V ExecutionPolicy /D Unrestricted /F
Start PowerShell.exe -Command "& '%~dpn0.ps1'"