Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 被调用函数未在函数内执行代码_Function_Powershell - Fatal编程技术网

Function 被调用函数未在函数内执行代码

Function 被调用函数未在函数内执行代码,function,powershell,Function,Powershell,基本上,我正在尝试创建一个脚本,以允许我的GPU miner选择运行miner的设置(选择与miner一起使用的GPU),以下是我为PowerShell脚本编写的代码: $choice = '0' function Get-Choice() { Write-Host 'Here are your choices:' Write-Host '1. All GPUs' Write-Host '2. GPU 0' Write-Host '3. GPU 1' $choice = Read-Host -P

基本上,我正在尝试创建一个脚本,以允许我的GPU miner选择运行miner的设置(选择与miner一起使用的GPU),以下是我为PowerShell脚本编写的代码:

$choice = '0'
function Get-Choice()
{
Write-Host 'Here are your choices:'
Write-Host '1. All GPUs'
Write-Host '2. GPU 0'
Write-Host '3. GPU 1'
$choice = Read-Host -Prompt 'Type the number and press enter to select what GPU(s) to mine with'
}

Get-Choice

function Set-Gpu0
{
Write-Host 'GPU 1 Chosen'
Start-Sleep -Seconds 2
miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 1 --intensity 64 --eexit 3
Set-Gpu0
}

function Set-Gpu1
{
Write-Host 'GPU 0 Chosen'
Start-Sleep -Seconds 2
miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 0 --intensity 64 --eexit 3
Set-Gpu1
}

function Set-All
{
Write-Host 'All GPUs Chosen'
Start-Sleep -Seconds 2
miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 0 1 --intensity 64 64 --eexit 3
Set-All 
}

function Is-Invalid
{
Write-Host 'Option is invalid. Pick again'
Get-Choice
}

If(-NOT($choice-eq1-or2-or3)){Is-Invalid}
Elseif($choice-eq1){Set-All}
Elseif($choice-eq2){Set-Gpu1}
Elseif($choice-eq3){Set-Gpu0}
该代码将要求用户通过输入数字从3个选项中选择一个,并在底部进行解释,并调用与该选项对应的函数。问题是,一旦它调用底部的函数,它就会退出脚本,而不运行函数中的任何代码。以下是运行时的外观:

PS C:\Windows\System32\WindowsPowerShell\v1.0> C:\Users\Colin\Documents\.GPU Miner\0.3.4b\Slushpool Miner.ps1
Here are your choices:
1. All GPUs
2. GPU 0
3. GPU 1
Type the number and press enter to select what GPU(s) to mine with: 3

PS C:\Windows\System32\WindowsPowerShell\v1.0>

我还尝试直接在“If”语句中运行代码,结果与上面的代码相同。有人能帮我理解一下我做错了什么吗?我是Powershell的新手,我不了解很多关于功能的在线指南,因为它们非常模糊/不符合我的需要。

至于新功能,这很好,但最好先通过MS Virtual Academy或YouTube进行一些视频培训,以了解所有功能,然后再针对文本/书籍参考。看,它使阅读更加简洁。对我们这些视觉学习者来说

然而,你已经把这件事复杂化了。只需给我们一个简单的if或case select语句。例如:

function Get-Choice()
{
    $choice = $null

    'Here are your choices (Select a number):'
    '1. All GPUs'
    '2. GPU 0'
    '3. GPU 1'

    $choice = Read-Host -Prompt 'Type the number and press enter to select what GPU(s) to mine with'
}

switch ($choice) 
    { 
        2 {
                    'GPU 1 Chosen'
                    Start-Sleep -Seconds 2
                    miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 1 --intensity 64 --eexit 3
                 } 
        3 {
                    'GPU 0 Chosen'
                    Start-Sleep -Seconds 2
                    miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 0 --intensity 64 --eexit 3
                 } 
        1  {
                    'All GPUs Chosen'
                    Start-Sleep -Seconds 2
                    miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 0 1 --intensity 64 64 --eexit 3
                 } 
        default  {
                    Write-Warning -Message 'Option is invalid. Pick again'
                    Get-Choice
                 }
    }

另外,在你的剧本中有一些东西我不明白。就像选择从一个给定的选项开始一个代码块,只是为了调用另一个

您的实现存在多个问题。这里有一个: 你不会写字

If(-NOT($choice -eq 1 -or 2-or 3)){Is-Invalid}
是的

if(-not($choice -eq 1 -or $choice -eq 2 -or $choice -eq 3)){Is-Invalid}
它可以更简洁地写成:

if(-not($choice -in 1..3)){Is-Invalid}
我能看到的使其工作的最小变化是:

[int]$choice = 0
function Get-Choice()
{
    Write-Host 'Here are your choices:'
    Write-Host '1. All GPUs'
    Write-Host '2. GPU 0'
    Write-Host '3. GPU 1'
    $choice = Read-Host -Prompt 'Type the number and press enter to select what GPU(s) to mine with'
    [int]$choice
}

function Set-Gpu0
{
    Write-Host 'GPU 1 Chosen'
    Start-Sleep -Seconds 2
    miner.exe --server us-east.zec.slushpool.com --port 4444 --user ColinAndress.Colin --pass x --cuda_devices 1 --intensity 64 --eexit 3
    Set-Gpu0
}

function Set-Gpu1
{
    Write-Host 'GPU 0 Chosen'
    Start-Sleep -Seconds 2
    miner.exe --server us-east.zec.slushpool.com --port 4444 --user     ColinAndress.Colin --pass x --cuda_devices 0 --intensity 64 --eexit 3
    Set-Gpu1
}

function Set-All
{
    Write-Host 'All GPUs Chosen'
    Start-Sleep -Seconds 2
    miner.exe --server us-east.zec.slushpool.com --port 4444 --user     ColinAndress.Colin --pass x --cuda_devices 0 1 --intensity 64 64 --eexit 3
    Set-All 
}

function Is-Invalid
{
    Write-Host 'Option is invalid. Pick again'
    Get-Choice # Not going to work
}

$choice = Get-Choice
If(-NOT($choice -in 1..3))
{
    Is-Invalid
}
Elseif($choice -eq 1)
{
    Set-All
}
Elseif($choice -eq 2)
{
    Set-Gpu1
}
Elseif($choice -eq 3)
{
    Set-Gpu0
}

然而,在一个无效的选择之后重新选择是不起作用的。

非常感谢您:D这是唯一一个真正起作用的选择。现在我需要实际调试如何启动miner lol。