Some errors in powershell script writing/demos

发布于:2025-09-13 ⋅ 阅读:(16) ⋅ 点赞:(0)

$fileSave=“d:\log”+(Get-Date).ToString(“yyyy-MM-dd_HH-mm-ss”)+“.txt”

if (!(Test-Path “d:\log”)) {
# New-Item -ItemType Directory -Path $fileSave -Force
New-Item -ItemType Directory -Path $fileSave
}

$win=0

for(i=1;i=1;i=1;i -le 100;$i++) {
$punch1 = Get-Random -Minimum 1 -Maximum 3
$punch2 = Get-Random -Minimum 1 -Maximum 3
outStr=“”

if ($punch1 -eq $punch2) {
    outStr="The"+$i+"time, draw match."
} elseif (punch1 -le $punch2) {
    $outStr = "The"+$i+"time, punch2 wins."
} else {
    $win++
    $outStr = "The"+$i+"time, punch1 wins."
}
outStr |out-File -FilePath $fileSave -encoding utf8 -Append

}

$winrate = win/100∗100"punch1winrateis"+win/100*100 "punch1 win rate is "+win/100100"punch1winrateis"+winrate+“%” |out-File -FilePath $fileSave -encoding utf8 -Append

检测错误



您的代码中有几个错误需要修复。以下是修正后的代码:

powershell
$fileSave="d:\log\"+(Get-Date).ToString("yyyy-MM-dd_HH-mm-ss")+".txt"

# 创建目录的路径应该是 "d:\log" 而不是 $fileSave
if (!(Test-Path "d:\log")) {
    New-Item -ItemType Directory -Path "d:\log" -Force
}

$win=0

for($i=1;$i -le 100;$i++) {
    $punch1 = Get-Random -Minimum 1 -Maximum 3
    $punch2 = Get-Random -Minimum 1 -Maximum 3
    $outStr=""  # 添加 $ 符号定义变量

    if ($punch1 -eq $punch2) {
        $outStr="The "+$i+" time, draw match."  # 添加空格使输出更可读
    } elseif ($punch1 -lt $punch2) {  # 修正为 $punch1 和 -lt (小于)
        $outStr = "The "+$i+" time, punch2 wins."
    } else {
        $win++
        $outStr = "The "+$i+" time, punch1 wins."
    }
    $outStr | Out-File -FilePath $fileSave -Encoding utf8 -Append  # 修正 Out-File
}

$winrate = $win/100*100
"punch1 win rate is "+$winrate+"%" | Out-File -FilePath $fileSave -Encoding utf8 -Append  # 修正 Out-File
主要错误修正:

目录创建问题:New-Item -ItemType Directory -Path $fileSave 应该使用目录路径 "d:\log" 而不是文件路径 $fileSave

变量定义:outStr="" 缺少 $ 符号,应该是 $outStr=""

条件判断:punch1 -le $punch2 缺少 $ 符号,应该是 $punch1 -le $punch2

但逻辑上应该使用 -lt (小于) 而不是 -le (小于等于),因为如果相等已经在第一个条件中处理了

cmdlet 名称:out-File 应该为 Out-File (注意大小写,虽然 PowerShell 不区分大小写,但标准写法是 Out-File)

参数名称:-encoding 应该为 -Encoding (标准写法)

输出格式:添加了空格使输出更易读

这些修正应该能让您的代码正常运行。

-force
您提到了 -Force 参数。在您的代码中,-Force 参数可以用于 New-Item cmdlet 来确保目录被创建,即使父目录不存在也会递归创建所有必需的目录。

以下是包含 -Force 参数的修正代码:

powershell
$fileSave = "d:\log\" + (Get-Date).ToString("yyyy-MM-dd_HH-mm-ss") + ".txt"

# 使用 -Force 参数确保目录被创建,即使父目录不存在
if (!(Test-Path "d:\log")) {
    New-Item -ItemType Directory -Path "d:\log" -Force
}

$win = 0

for ($i = 1; $i -le 100; $i++) {
    $punch1 = Get-Random -Minimum 1 -Maximum 3
    $punch2 = Get-Random -Minimum 1 -Maximum 3
    $outStr = ""

    if ($punch1 -eq $punch2) {
        $outStr = "The " + $i + " time, draw match."
    } elseif ($punch1 -lt $punch2) {
        $outStr = "The " + $i + " time, punch2 wins."
    } else {
        $win++
        $outStr = "The " + $i + " time, punch1 wins."
    }
    $outStr | Out-File -FilePath $fileSave -Encoding utf8 -Append
}

$winrate = $win / 100 * 100
"punch1 win rate is " + $winrate + "%" | Out-File -FilePath $fileSave -Encoding utf8 -Append
关于 -Force 参数的作用:

创建目录:如果目录不存在,会创建它

递归创建:如果父目录也不存在,会递归创建所有必需的父目录

覆盖现有项:如果路径已存在文件或目录,-Force 会尝试覆盖它(但对于目录,通常不会删除已有内容)

实际上,您可以简化代码,完全去掉 Test-Path 检查:

powershell
$fileSave = "d:\log\" + (Get-Date).ToString("yyyy-MM-dd_HH-mm-ss") + ".txt"

# 直接使用 -Force 创建目录,无需先检查是否存在
New-Item -ItemType Directory -Path "d:\log" -Force | Out-Null

# 剩余代码保持不变...
这样更加简洁,-Force 参数会处理所有必要的目录创建操作