我目前正在编写一个脚本来提示用户输入,但是想要继续提示用户,直到输入工作站\ IP地址或用户名。这将被实现为更大的更强大的脚本,但我无法使这部分工作。我的语法是关闭还是我误解了逻辑,可能两者都有。
D????-*
是我们的企业工作站命名标准(即DOUAW-7811652
)*-admin
指的是较大脚本到目前为止,代码看起来像这样:
Function Admin{
Write-host "Admin Accounts are not supported" -ForegroundColor Red
}
Function Workstation{
Write-Host "Workstation $name"
$workstation = $true
}
Function UserName{
Write-Host "User Name $name"
$UserName = $true
}
DO {
$Name = Read-Host "Please enter the User Name or Workstation(or IP)"
switch -Wildcard ($name){
"*-admin" {Admin}
"D????-*"{Workstation}
"*.*.*.*"{Workstation}
Default {UserName}
}
}
until ($UserName -eq "$true" -or $workstation -eq "$true")
Read-Host "Press any key to exit"
答案 0 :(得分:0)
可能你的意思是:
until ($UserName -eq $true -or $workstation -eq $true)
(注意$true
周围没有引号。)
答案 1 :(得分:0)
(虽然我可能没有像这样编写它,但我只修复了代码,这样你就可以摆脱无限循环了)
您可能需要类似
的内容Function Workstation{
Write-Host "Workstation $name"
return $true
}
Function UserName{
Write-Host "User Name $name"
return $true
}
DO {
$Name = Read-Host "Please enter the User Name or Workstation(or IP)"
switch -Wildcard ($name){
"*-admin" {Admin}
"D????-*"{$workstation = Workstation}
"*.*.*.*"{$workstation = Workstation}
Default {$UserName = UserName}
}
} until ($UserName -eq "$true" -or $workstation -eq "$true")
答案 2 :(得分:0)
我只想说这是一个很好的使用高级功能。以满足所有验证和强制要求的方式定义参数。这样的事情可以让你非常接近你想要的东西。
Function Do-Something
{
[CmdletBinding(DefaultParameterSetName="Username")]
param(
[Parameter(
Mandatory = $true,
HelpMessage = "Please enter a username",
Position = 0,
ParameterSetName = "Username",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[string[]]$Username,
[Parameter(
Mandatory = $true,
HelpMessage = "Please enter a computername",
Position = 0,
ParameterSetName = "Computername",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[ValidatePattern('D\w\w\w\w-|(\d+\.){3}\d+')]
[string[]]$ComputerName
)
process
{
if($Username){Username}
if($ComputerName){Computername}
}
}
Show-Command Do-Something