首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell脚本在Windows 8上运行正常,但在Windows 7上运行不正常

Powershell脚本在Windows 8上运行正常,但在Windows 7上运行不正常
EN

Stack Overflow用户
提问于 2013-06-03 05:19:39
回答 2查看 768关注 0票数 2

我是powershell的新手,我不确定我做错了什么。它在我的Windows8电脑上运行得很好,但当我把它发送给其他人(他有Windows7;为他创建了这个)时,他得到了一个不允许运行脚本的错误。

已尝试使用-ExecutionPolicy RemoteSigned,但仍未成功。

代码语言:javascript
复制
##################
<# CONFIG START #>
##################
#replace the path with your steam path. For example: C:\Program Files (x86)\Steam\Steam.exe
$steam_path = "C:\Program Files (x86)\Steam\steam.exe"

#You can change it to Ethernet 1 or Ethernet 2 or Ethernet 3 etc depending on which adapter you want to disable.
#If you have custom name for them (you can rename them from control panel), have to use that name.
$adapter_name = "Ethernet 1"

<#
What program to run.
1: Steam Dota 2
2: Steam CS
3: Steam CSS
4: Steam CSGO
5: Custom Program 1
6: Custom Program 2
7: Custom Program 3
#>
$game_id = "5"

<# Custom Program path and arguments#>
$cp1_path = "C:\Program Files (x86)\counter-strike source\css.exe"
$cp1_arg = " "

$cp2_path = ""
$cp2_arg = " "

$cp3_path = ""
$cp2_arg = " "

$delay = 20


################
<# CONFIG END #>
################


"Checking admin permissions..."

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    "Administrator permissions required."
    $arguments = '-ExecutionPolicy RemoteSigned -file "' + $myinvocation.mycommand.definition + '"'
#    $arguments
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
}


"Exiting Steam..."
Start-Process -FilePath $steam_path -ArgumentList "-shutdown" -Wait:$true

Start-Sleep -s 2

"Disabling Network Adapter..."
Disable-NetAdapter -Name $adapter_name -Confirm:$false

Start-Sleep -s 5

"Starting Game..."

Switch($game_id)
{

    1
    {
        Start-Process -filepath "steam://rungameid/570"
    }

    2
    {
        Start-Process -filepath "steam://rungameid/10"
    }

    3
    {
        Start-Process -filepath "steam://rungameid/240"
    }

    4
    {
        Start-Process -filepath "steam://rungameid/730"
    }

    5
    {
        Start-Process $cp1_path -ArgumentList $cp1_arg
    }

    6
    {
        Start-Process $cp2_path -ArgumentList $cp2_arg
    }

    7
    {
        Start-Process $cp3_path -ArgumentList $cp3_arg
    }
}

Start-Sleep -s $delay

"Enabling Network Adapter..."
Enable-NetAdapter $adapter_name -Confirm:$false

exit
EN

回答 2

Stack Overflow用户

发布于 2013-06-03 06:55:01

如果你把脚本发给他了,那么RemoteSigned就做得很好了。他从远程(从你那里)得到了脚本,但它没有签名,所以它不会被执行。

告诉你的朋友导航到Windows资源管理器中的ps1脚本,右键单击,然后选择“解除阻止”。如果PowerShell实例已经无法运行脚本,那么他将需要重新启动该实例,因为这种信息是由Windows缓存的。

票数 3
EN

Stack Overflow用户

发布于 2013-06-03 06:53:30

该错误表明您没有正确设置执行策略。首先,您以管理员身份运行Powershell。为此,右键单击开始菜单中的Powershell图标,然后单击“以管理员身份运行”。接下来,您必须在Powershell中运行以下命令:

代码语言:javascript
复制
Set-ExecutionPolicy RemoteSigned

系统将提示您允许更改。键入"Y“,然后按return。

最后,尝试运行您的脚本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16887434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档