我没有在这个 powershell 脚本中得到预期的输出
I'm not getting the expected outputs in this powershell script
我在 powershell 中制作了这个脚本,但我没有做正确的事情(我正在尝试自学 uwu)
这是有问题的脚本。
function Get-DesiredProcess {
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
Switch ($DesiredProcess)
{
WUP {$ChosenProcess=Install-WUpdate}
MDP {$ChosenProcess=Set-Proxy}
}
If ($DesiredProcess -eq $null) {
Write-Error "You must specify an action!"
return Get-DesiredProcess
}
Else {
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
}
}
Get-DesiredProcess
Write-Output $DesiredProcess
Write-Output $ChosenProcess
最后的 2 个“Write-Output”仅用于测试它是否正确注册了变量(剧透,它没有)
当Read-Host没有被回答时,它应该输出“You specify an action!”但什么都不做:
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification):
PS C:\Users\user1\Documents\scriptsfiles>
并且在做出选择的时候,应该保留这部分的变量:
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
而不是直接执行选择的进程...
提前致谢!
阅读您的描述后我了解到,您需要将所有变量声明为全局变量,否则您需要在函数内部访问这些变量。无法从函数外部访问函数变量。根据你的描述,我的理解是你的代码是这样的:
function Get-DesiredProcess
{
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess)
{
1 {$ChosenProcess="Install-WUpdate"}
2 {$ChosenProcess="Set-Proxy"}
}
If ($DesiredProcess -eq [string]::empty)
{
Write-Error "You must specify an action!"
Get-DesiredProcess
}
Else
{
Set-Variable -Name "DesiredProcessName" -Value "$ChosenProcess"
Write-host "You chose this: "$DesiredProcessName
}
}
Get-DesiredProcess
如果您希望您的函数将新值设置为在函数外部定义的变量,请在函数内部使用作用域,因此
$ChosenProcess = 'Install-WUpdate'
--> $script:ChosenProcess = 'Install-WUpdate'
.
但是,更容易理解的是让您的函数输出其他代码可以处理的内容。在这种情况下,由于您既想要用户输入的内容,又想要为该输入选择的过程,我建议让函数输出一个 object ,它具有以下两个属性:
function Get-DesiredProcess {
# inside the function both variables have LOCAL scope
$ChosenProcess = $null # initialize to nothing, or an empty string
$DesiredProcess = Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess) {
'WUP' {$ChosenProcess = 'Install-WUpdate'}
'MDP' {$ChosenProcess = 'Set-Proxy'}
default {
Write-Warning "You did not specify an accepted action. Type 'WUP' or 'MDP'"
Get-DesiredProcess
}
}
# only output if there is a matching chosen process
if (![string]::IsNullOrWhiteSpace($ChosenProcess)) {
# now have the function return both what the user typed and what the chosen action should be
[PsCustomObject]@{
DesiredProcess = $DesiredProcess
ChosenProcess = $ChosenProcess
}
}
}
$result = Get-DesiredProcess
使用它会输出类似
的内容
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): 7
WARNING: You did not specify an accepted action. Type 'WUP' or 'MDP'
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): wup
DesiredProcess ChosenProcess
-------------- -------------
wup Install-WUpdate
现在,下面的代码可以简单地作用于变量 $result.ChosenProcess
中的内容
我在 powershell 中制作了这个脚本,但我没有做正确的事情(我正在尝试自学 uwu)
这是有问题的脚本。
function Get-DesiredProcess {
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
Switch ($DesiredProcess)
{
WUP {$ChosenProcess=Install-WUpdate}
MDP {$ChosenProcess=Set-Proxy}
}
If ($DesiredProcess -eq $null) {
Write-Error "You must specify an action!"
return Get-DesiredProcess
}
Else {
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
}
}
Get-DesiredProcess
Write-Output $DesiredProcess
Write-Output $ChosenProcess
最后的 2 个“Write-Output”仅用于测试它是否正确注册了变量(剧透,它没有)
当Read-Host没有被回答时,它应该输出“You specify an action!”但什么都不做:
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification):
PS C:\Users\user1\Documents\scriptsfiles>
并且在做出选择的时候,应该保留这部分的变量:
Set-Variable -Name "DesiredProcess" -Value "ChosenProcess"
而不是直接执行选择的进程...
提前致谢!
阅读您的描述后我了解到,您需要将所有变量声明为全局变量,否则您需要在函数内部访问这些变量。无法从函数外部访问函数变量。根据你的描述,我的理解是你的代码是这样的:
function Get-DesiredProcess
{
$DesiredProcess=Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess)
{
1 {$ChosenProcess="Install-WUpdate"}
2 {$ChosenProcess="Set-Proxy"}
}
If ($DesiredProcess -eq [string]::empty)
{
Write-Error "You must specify an action!"
Get-DesiredProcess
}
Else
{
Set-Variable -Name "DesiredProcessName" -Value "$ChosenProcess"
Write-host "You chose this: "$DesiredProcessName
}
}
Get-DesiredProcess
如果您希望您的函数将新值设置为在函数外部定义的变量,请在函数内部使用作用域,因此
$ChosenProcess = 'Install-WUpdate'
--> $script:ChosenProcess = 'Install-WUpdate'
.
但是,更容易理解的是让您的函数输出其他代码可以处理的内容。在这种情况下,由于您既想要用户输入的内容,又想要为该输入选择的过程,我建议让函数输出一个 object ,它具有以下两个属性:
function Get-DesiredProcess {
# inside the function both variables have LOCAL scope
$ChosenProcess = $null # initialize to nothing, or an empty string
$DesiredProcess = Read-Host "Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification)"
switch ($DesiredProcess) {
'WUP' {$ChosenProcess = 'Install-WUpdate'}
'MDP' {$ChosenProcess = 'Set-Proxy'}
default {
Write-Warning "You did not specify an accepted action. Type 'WUP' or 'MDP'"
Get-DesiredProcess
}
}
# only output if there is a matching chosen process
if (![string]::IsNullOrWhiteSpace($ChosenProcess)) {
# now have the function return both what the user typed and what the chosen action should be
[PsCustomObject]@{
DesiredProcess = $DesiredProcess
ChosenProcess = $ChosenProcess
}
}
}
$result = Get-DesiredProcess
使用它会输出类似
的内容Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): 7
WARNING: You did not specify an accepted action. Type 'WUP' or 'MDP'
Welcome to de advanced task manager! Choose an action to do: 1. WUP (WindowsUpdate) 2. MDP (ProxyModification): wup
DesiredProcess ChosenProcess
-------------- -------------
wup Install-WUpdate
现在,下面的代码可以简单地作用于变量 $result.ChosenProcess