首页 > 解决方案 > 无法让它提供正确的输出

问题描述

我有这个功能,但我无法让它工作,$DecimalConversion 输出没有出来。我想我有一些语法错误。

function Get-DecimalNumber(){
$FileCheck = Test-Path "C:\Conversions\conversions.csv"
if($FileCheck){
Do
{
[int]$GetDecimal = Read-host "Write a number between 1-255" | Out-Null
}
while ($GetDecimal -notmatch '\d{1,3}' -or (1..255) -notcontains $GetDecimal)
$DecimalConversion= "{0:X}" -f $GetDecimal
$DecimalConversion
}
else{Write-Warning "Can not find conversions.csv, creating now under C:\Conversions\"; New-Item "C:\Conversions\conversions.csv" -Force | Out-Null}
}
$getfunction=Get-DecimalNumber

标签: functionpowershell

解决方案


您可能可以使用更好的while条件。out-null但是,您的问题是由于read-host.

如果您使用它,$GetDecimal将不会获得您传入的值,因为它out-null是在分配发生之前处理的。只需将其删除。它应该工作。


推荐阅读