AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1277488

Maslie's questions

Martin Hope
Maslie
Asked: 2024-07-12 16:58:13 +0800 CST

Powershell 程序,显示功能不起作用 [已解决]

  • 5

大家早上好,

我在使用 powershell 脚本时遇到了问题。

语境

我正在尝试创建一个程序来在 powershell 中编辑 tf2 pop 文件,现在我正在尝试创建一个程序来打开一个 txt 文件,显示每个 wavespawn 的内容以及一个用于复制 wavespawn 的按钮。

这是我目前在获得一些帮助后编写的 powershell 脚本。

# Import necessary assemblies for Windows Forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Function to create OpenFileDialog
function Show-OpenFileDialog {
    $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.InitialDirectory = [System.Environment]::GetFolderPath('MyDocuments')
    $openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    $openFileDialog.Title = "Select a Text File"
    $result = $openFileDialog.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        return $openFileDialog.FileName
    } else {
        return $null
    }
}

# Function to load file content and extract WaveSpawn sections
function Load-FileContent {
    param (
        [string]$filePath,
        [System.Windows.Forms.ListBox]$listBox,
        [ref]$waveSpawnDetails
    )
    $listBox.Items.Clear()
    $waveSpawnDetails.Value.Clear()
    if (Test-Path $filePath) {
        $content = Get-Content -Path $filePath -Raw
        # Extract all WaveSpawn sections with a more robust regex pattern
        $waveSpawns = [regex]::Matches($content, 'WaveSpawn\s*\{(?:[^\{\}]*|(?<Open>\{)|(?<-Open>\}))*\}(?(Open)(?!))')
        $waveSpawns | ForEach-Object {
            $waveSpawn = $_.Value
            if ($waveSpawn -match 'Name\s+(\w+)') {
                $name = $matches[1]
                $listBox.Items.Add($name)
                $waveSpawnDetails.Value[$name] = $waveSpawn
            }
        }
    } else {
        [System.Windows.Forms.MessageBox]::Show("File not found: $filePath", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
}

# Function to display details of the selected WaveSpawn
function Show-WaveSpawnDetails {
    param (
        [string]$waveSpawnName,
        [System.Windows.Forms.TextBox]$textBox,
        [hashtable]$waveSpawnDetails
    )
    if ($waveSpawnDetails.ContainsKey($waveSpawnName)) {
        $textBox.Text = $waveSpawnDetails[$waveSpawnName]
    } else {
        $textBox.Text = "WaveSpawn details not found."
    }
}

# Main form
$form = New-Object Windows.Forms.Form
$form.Text = "WaveSpawns"
$form.Size = New-Object Drawing.Size(800, 600)

# ListBox to display WaveSpawns
$listBox = New-Object Windows.Forms.ListBox
$listBox.Size = New-Object Drawing.Size(300, 500)
$listBox.Location = New-Object Drawing.Point(10, 10)

# TextBox to display selected WaveSpawn details
$textBox = New-Object Windows.Forms.TextBox
$textBox.Size = New-Object Drawing.Size(450, 500)
$textBox.Location = New-Object Drawing.Point(320, 10)
$textBox.Multiline = $true
$textBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical

# Button to copy WaveSpawn details to clipboard
$copyButton = New-Object Windows.Forms.Button
$copyButton.Text = "Copy"
$copyButton.Size = New-Object Drawing.Size(75, 23)
$copyButton.Location = New-Object Drawing.Point(320, 520)
$copyButton.Add_Click({
    Set-Clipboard -Value $textBox.Text
    [System.Windows.Forms.MessageBox]::Show("WaveSpawn copied to clipboard!")
})

# Button to open file
$openButton = New-Object Windows.Forms.Button
$openButton.Text = "Open File"
$openButton.Size = New-Object Drawing.Size(75, 23)
$openButton.Location = New-Object Drawing.Point(10, 520)
$openButton.Add_Click({
    $filePath = Show-OpenFileDialog
    if ($filePath) {
        Load-FileContent -filePath $filePath -listBox $listBox -waveSpawnDetails ([ref]$waveSpawnDetails)
    }
})

# Add event handler for ListBox selection
$listBox.SelectedIndexChanged.Add({
    $selectedWaveSpawnName = $listBox.SelectedItem.ToString()
    Show-WaveSpawnDetails -waveSpawnName $selectedWaveSpawnName -textBox $textBox -waveSpawnDetails $waveSpawnDetails
})

# Add controls to form
$form.Controls.Add($listBox)
$form.Controls.Add($textBox)
$form.Controls.Add($copyButton)
$form.Controls.Add($openButton)

# Initialize hashtable to store WaveSpawn details
$global:waveSpawnDetails = @{}

# Show the form
$form.ShowDialog()

测试“pop”文件

//This mission is a dummy mission and is probably not working, it just here to test the powershell script in "real condition"

#base robot_standard.pop
#base robot_giant.pop


WaveSchedule
{
    StartingCurrency 900
    RespawnWaveTime 4
    CanBotsAttackWhileInSpawnRoom no
    
    Mission
    {
        Objective   DestroySentries
        BeginAtWave 1
        RunForThisManyWaves 3
        InitialCooldown 30
        CooldownTime    30
        DesiredCount    1
        Where   spawnbot_mission_sentrybuster
        
        TFBot
        {
            Template    T_TFBot_SentryBuster
        }
    }   
    
    Wave    //wave 1    
    {

        StartWaveOutput
        {
            Target  wave_start_relay
            Action  Trigger
        }
        DoneOutput
        {
            Target  wave_finished_relay
            Action  Trigger
        }
        
        WaveSpawn
        {
            Name    wave1
            TotalCurrency   300
            TotalCount  4
            MaxActive   6
            SpawnCount  1
            WaitBeforeStarting  0
            WaitBetweenSpawns   12
            Where   spawnbot

            TFBot
            {
                Template    T_TFBot_Giant_Pyro
                Tag bot_giant
                Attributes  AlwaysFireWeapon
            }   
        }
        
        WaveSpawn
        {
            Name    wave1
            TotalCurrency   100
            TotalCount  24
            MaxActive   12
            SpawnCount  2
            WaitBeforeStarting  2
            WaitBetweenSpawns   2.5
            Where   spawnbot_side
            
            Squad
            {
                TFBot
                {
                
                    Class Soldier
                    Skill normal
                }           
                TFBot
                {
                    Template    T_TFBot_Medic_QuickFix
                    Tag bot_squad_member
                    
                    ItemAttributes
                    {
                        ItemName "The Quick-Fix"
                        "ubercharge rate bonus" 0.1
                    }
                }
            }   
        }   
        WaveSpawn
        {
            Name    wave2
            WaitForAllSpawned w1a
            TotalCurrency   100
            TotalCount  3
            MaxActive   3
            SpawnCount  1
            WaitBeforeStarting  7
            WaitBetweenSpawns   7
            Where   spawnbot

            TFBot
            {
                Template    T_TFBot_Giant_Demoman
                Tag bot_giant
            }   
        }

        WaveSpawn
        {
            Name    wave2
            WaitForAllSpawned w1a
            TotalCurrency   300
            TotalCount  1
            MaxActive   1
            SpawnCount  1
            WaitBeforeStarting  2
            WaitBetweenSpawns   1

            FirstSpawnOutput
            {
                Target boss_spawn_relay
                Action Trigger
            }
            
            Tank
            {
                Name "Tank"
                Health 15000
                Speed 70
                StartingPathTrackNode tank_path_1
                OnKilledOutput
                {
                    Target boss_dead_relay
                    Action Trigger
                }
                OnBombDroppedOutput
                {
                    Target boss_deploy_relay
                    Action Trigger
                }
            }
        }
        
        WaveSpawn
        {
            Name    wave3
            WaitForAllSpawned w1b
            TotalCurrency   50
            TotalCount  2
            MaxActive   2
            SpawnCount  1
            WaitBeforeStarting  4
            WaitBetweenSpawns   7
            Where   spawnbot_side

            TFBot
            {
                Template    T_TFBot_Giant_Scout_FAN
                Tag bot_giant
            }   
        }       
        
        WaveSpawn
        {
            Name    wave3
            WaitForAllSpawned w1b
            Support 1
            TotalCurrency   50
            TotalCount  50
            MaxActive   10
            Support Limited
            SpawnCount  5
            WaitBeforeStarting  7
            WaitBetweenSpawns   1
            Where   spawnbot_invasion

            TFBot
            {
                Class Demoman
            }   
        }
    }           
}   

结果代码的当前结果

此外,当我尝试复制 wavespawn 时,这里还出现了错误消息,这是我收到的唯一错误。

mpossible d’appeler une méthode dans une expression Null.
Au caractère P:\Document\Fichier texte\Switch_supervision - Copie.ps1:99 : 1
+ $listBox.SelectedIndexChanged.Add({
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

有人能帮我弄清楚脚本发生了什么以及为什么两个主要功能不起作用吗?

更新:如果有人感兴趣的话@Someone 已经找到了解决方案,这里有有效的代码:

# Import necessary assemblies for Windows Forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Function to create OpenFileDialog
function Show-OpenFileDialog {
    $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.InitialDirectory = [System.Environment]::GetFolderPath('MyDocuments')
    $openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    $openFileDialog.Title = "Select a Text File"
    $result = $openFileDialog.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        return $openFileDialog.FileName
    } else {
        return $null
    }
}

# Function to load file content and extract WaveSpawn sections
function Load-FileContent {
    param (
        [string]$filePath,
        [System.Windows.Forms.ListBox]$listBox,
        [ref]$waveSpawnDetails
    )
    $listBox.Items.Clear()
    $waveSpawnDetails.Value.Clear()
    if (Test-Path $filePath) {
        $content = Get-Content -Path $filePath -Raw
        # Extract all WaveSpawn sections with a more robust regex pattern
        $waveSpawns = [regex]::Matches($content, 'WaveSpawn\s*\{(?:[^\{\}]*|(?<Open>\{)|(?<-Open>\}))*\}(?(Open)(?!))')
        $waveSpawns | ForEach-Object {
            $waveSpawn = $_.Value
            if ($waveSpawn -match 'Name\s+(\w+)') {
                $name = $matches[1]
                $listBox.Items.Add($name)
                $waveSpawnDetails.Value[$name] = $waveSpawn
            }
        }
    } else {
        [System.Windows.Forms.MessageBox]::Show("File not found: $filePath", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
}

# Function to display details of the selected WaveSpawn
function Show-WaveSpawnDetails {
    param (
        [string]$waveSpawnName,
        [System.Windows.Forms.TextBox]$textBox,
        [hashtable]$waveSpawnDetails
    )
    if ($waveSpawnDetails.ContainsKey($waveSpawnName)) {
        $textBox.Text = $waveSpawnDetails[$waveSpawnName]
    } else {
        $textBox.Text = "WaveSpawn details not found."
    }
}

# Main form
$form = New-Object Windows.Forms.Form
$form.Text = "WaveSpawns"
$form.Size = New-Object Drawing.Size(800, 600)

# ListBox to display WaveSpawns
$listBox = New-Object Windows.Forms.ListBox
$listBox.Size = New-Object Drawing.Size(300, 500)
$listBox.Location = New-Object Drawing.Point(10, 10)

# TextBox to display selected WaveSpawn details
$textBox = New-Object Windows.Forms.TextBox
$textBox.Size = New-Object Drawing.Size(450, 500)
$textBox.Location = New-Object Drawing.Point(320, 10)
$textBox.Multiline = $true
$textBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical

# Button to copy WaveSpawn details to clipboard
$copyButton = New-Object Windows.Forms.Button
$copyButton.Text = "Copy"
$copyButton.Size = New-Object Drawing.Size(75, 23)
$copyButton.Location = New-Object Drawing.Point(320, 520)
$copyButton.Add_Click({
    Set-Clipboard -Value $textBox.Text
    [System.Windows.Forms.MessageBox]::Show("WaveSpawn copied to clipboard!")
})

# Button to open file
$openButton = New-Object Windows.Forms.Button
$openButton.Text = "Open File"
$openButton.Size = New-Object Drawing.Size(75, 23)
$openButton.Location = New-Object Drawing.Point(10, 520)
$openButton.Add_Click({
    $filePath = Show-OpenFileDialog
    if ($filePath) {
        Load-FileContent -filePath $filePath -listBox $listBox -waveSpawnDetails ([ref]$waveSpawnDetails)
    }
})

# Add event handler for ListBox selection
$listBox.Add_SelectedIndexChanged({
    $selectedWaveSpawnName = $listBox.SelectedItem.ToString()
    Show-WaveSpawnDetails -waveSpawnName $selectedWaveSpawnName -textBox $textBox -waveSpawnDetails $waveSpawnDetails
})

# Add controls to form
$form.Controls.Add($listBox)
$form.Controls.Add($textBox)
$form.Controls.Add($copyButton)
$form.Controls.Add($openButton)

# Initialize hashtable to store WaveSpawn details
$global:waveSpawnDetails = @{}

# Show the form
$form.ShowDialog() 
windows
  • 1 个回答
  • 38 Views
Martin Hope
Maslie
Asked: 2024-04-26 16:56:01 +0800 CST

使用powershell导出excel数据进行robocopy

  • 6

我手上有一个 300 行的 excel 表,其中包含用于 robocopy 的源路径和目标路径,为了避免将所有路径复制粘贴到 .bat 文件上,我尝试制作此 powershell 脚本:

# Import the Excel module
Import-Module ImportExcel

# Path to the Excel file
$excelPath = 'classeur.xlsx'

# Import data from the Excel file and check for null values
$data = Import-Excel -Path $excelPath
if ($data -eq $null) {
    Write-Host "No data was imported from the file. Please check if the file is empty."
    return
}

# Display imported data for verification
Write-Host "Imported data preview:"
$data | Format-Table -AutoSize

# File to store the source and destination paths
$outputFile = 'robocopy.txt'
if (Test-Path $outputFile) {
    Remove-Item $outputFile
}

# Iterate over each row of data
foreach ($row in $data) {
    $source = $row.Source
    $destination = $row.Destination

    # Check if either source or destination is empty
    if ([string]::IsNullOrWhiteSpace($source) -or [string]::IsNullOrWhiteSpace($destination)) {
        Write-Host "Empty source or destination found, skipping..."
    } else {
        # Format the line to write to the file
        $lineToWrite = "Source: `"$source`" - Destination: `"$destination`""
        Write-Host "Writing to file: $lineToWrite"
        $lineToWrite | Out-File -FilePath $outputFile -Append -Encoding UTF8
    }
}

Write-Host "All lines processed. Paths are stored in $outputFile"

当然,我复制了 excel 文件并重新构建它,以方便脚本读取

构造示例

来源 目的地
源文件路径 目的地 filapath
源文件路径 目的地 filapath

我期待的结果如下

SET _source1="Filepath to source"

SET  _destsge1="Filepath to destination"

SET _source2="Filepath to source"

SET  _destsge2="Filepath to destination"

ETC...

我目前得到的结果

Empty source or destination found, skipping...
Empty source or destination found, skipping...
Empty source or destination found, skipping...
Empty source or destination found, skipping...

我检查了什么:

  • powershell正确读取了excel,就是这样。
  • 我的PS版本是5.1.22621.2506
  • 以管理员身份执行脚本。
  • Excel 版本:版本 2403(内部版本 17425.20146)

我想指出的是,txt 文件和 excel 文件位于具有脚本的同一文件夹中。

有人可以知道为什么脚本无法读取数据我找不到任何特定于此问题的内容:

我使用的来源:https://www.sharepointdiary.com/2021/03/import-excel-file-in-powershell.html# :~:text=To%20import%20an%20Excel%20file%2C%20follow%20these %20steps%3A,%20Excel%20文件。%20For%20example%3A%20Import-Excel%20-Path%20C%3APathToExcelFile.xlsx

https://www.it-connect.fr/comment-manipuler-des-fichiers-excel-avec-powershell/

microsoft-excel
  • 1 个回答
  • 29 Views
Martin Hope
Maslie
Asked: 2023-06-27 15:34:21 +0800 CST

电脑一直告诉我打印机存在

  • 5

我必须在京瓷打印机上运行不同的测试,包括在我的计算机上多次重新安装打印机以运行不同的测试。

但是,由于某种原因,即使在我尝试重新安装打印机时从计算机中删除打印机后,计算机仍然告诉我端口已具有相同的名称。

我在这里缺少什么?纠正这个问题的解决方案是什么?

我运行的是最新的Windows 10版本,打印机在线并且多个用户可以使用它。我对打印机执行 ping 操作并访问网页没有问题。

windows-10
  • 1 个回答
  • 16 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve