首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell脚本未引用存储在变量中的值

PowerShell脚本未引用存储在变量中的值
EN

Stack Overflow用户
提问于 2018-02-02 05:01:40
回答 1查看 228关注 0票数 0

我在让脚本在整个脚本中使用存储在$computername变量中的值时遇到了问题。该值用于在我们网络上的远程计算机上运行命令。该脚本仅使用存储在变量中的值一次,但随后的两个if else命令在本地计算机上运行,而不是在远程计算机上运行。

下面是.ps1脚本:

代码语言:javascript
复制
$computername = Read-Host "What computer do you want to check hibernation status for?"
Invoke-Command -Computer $computername {
    Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Power -Name HibernateEnabled
}

$hiberstatus = Read-Host "Would you like to change hibernation status for this machine? Use 'y' for Yes and 'n' for No."

if ($hiberstatus -eq 'y') {
    $hiberchange = Read-Host "Would you like to enable or disable hibernation? Use 'en' for enable and 'dis' for disable."
    if ($hiberchange -eq 'en') {
        Enter-PSSession $computername
        powershell -Command "Start-Process 'powercfg.exe' -Verb runAs -Argumentlist '/h on'"
        Exit-PSSession
    } elseif ($hiberchange -eq 'dis') {
        Enter-PSSession $computername
        powershell -Command "Start-Process 'powercfg.exe' -Verb runAs -Argumentlist '/h off'"
        Exit-PSSession
    }
} elseif ($hiberstatus -eq 'n') {
    exit
}

$hiberupdate = Read-Host "Would you like to confirm the changes made? Use 'y' for Yes and 'n' for No."

if ($hiberupdate -eq 'y') {
    Invoke-Command -Computer $computername {
        Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Power -Name HibernateEnabled
    }
} elseif ($hiberupdate -eq 'n') {
    Write-Output 'Terminating powercfg sessions!'
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-02 17:32:45

下面是我会怎么做:

代码语言:javascript
复制
if ($hiberstatus -eq 'y') {
    $hiberchange = Read-Host "Would you like to enable or disable hibernation? Use 'en' for enable and 'dis' for disable."
    if ($hiberchange -eq 'en') {
        Invoke-Command $ComputerName {"Start-Process 'powercfg.exe' -Verb runAs -Argumentlist '/h on'"}
    } elseif ($hiberchange -eq 'dis') {
        Invoke-Command $ComputerName {"Start-Process 'powercfg.exe' -Verb runAs -Argumentlist '/h off'"}
    }
} elseif ($hiberstatus -eq 'n') {
    exit
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48571634

复制
相关文章

相似问题

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