Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据powershell中的环境读取和解析文本文件中的值?_Powershell - Fatal编程技术网

如何根据powershell中的环境读取和解析文本文件中的值?

如何根据powershell中的环境读取和解析文本文件中的值?,powershell,Powershell,我是powershell的新手,正在编写脚本,其中包含以下格式的txt文件 文件名abc.txt ABC=WINDOW01 ABC_NCD_PP CDE=WINDOW02 CDE_NN_01 EFG=WINDO03 DD_PP_01 下面我正在尝试我的脚本 function Show-Menu { param ( [string]$Title = 'My Menu' ) Clear-Host Write-Host "=============

我是powershell的新手,正在编写脚本,其中包含以下格式的txt文件

文件名abc.txt

ABC=WINDOW01
ABC_NCD_PP

CDE=WINDOW02
CDE_NN_01

EFG=WINDO03
DD_PP_01
下面我正在尝试我的脚本

function Show-Menu
{
    param (
        [string]$Title = 'My Menu'
    )
    Clear-Host
    Write-Host "================ $Title ================"
    Write-Host "1: Press '1' to list all the Window Boxes"
    Write-Host "Q: Press 'Q' to quit."
}

function Vm-List 
{
    Write-Output "Below are the window boxes"
    $serverlist = @(get-content  -Path "k:\abc.txt")
    $counter = 0
    foreach ($server in $serverlist)
    {
      $counter++
    write-host ""$counter": Press '"$counter"' to select : $server"
    }

}

do
{
     Show-Menu
     $input = Read-Host "Please make a selection"
     switch ($input)
     {
           '1' {
                Vm-List

           } 'q' {
                return
           }
     }
     pause
}
until ($input -eq 'q')
我想当用户按1时,它应该以下面的格式显示所有窗口框

1: Press '1' to select ABC window box WINDOW01
2: Press '2' to select CDE window box WINDOW02
3: Press '3' to select EFG window box WINDOW03
Q: Press 'q' to quit
Please make a selection:
一旦我从上面选择了任何选项,它就会根据该框显示值。例如,如果我选择1,它就会显示(这些值可能更大)


你把事情搞得太复杂了。努力。此外,这里有不止一个问题/问题案例,作为新手,您正在尝试构建一个MDI控制台应用程序。我认为这对初学者来说有点过分

你没有得到你想要的东西的原因是:

  • 您根本没有解析该文本文件以获取所需的文本 如您所示显示
  • 您确实没有为第二个菜单配置MDI方法 一点也不
  • 刚接触某样东西是很好的,但至少在尝试使用它之前不需要接受一些提升训练,不是这样的。尤其是网上有大量免费视频、教程、博客、示例代码、参考书等。使用系统内置的帮助文件

    # All Help topics and locations
    Get-Help about_*
    Get-Help about_Functions
    
    Get-Help about* | Select Name, Synopsis
    
    Get-Help about* | 
    Select-Object -Property Name, Synopsis |
    Out-GridView -Title 'Select Topic' -OutputMode Multiple |
    ForEach-Object { Get-Help -Name $_.Name -ShowWindow }
    
    explorer "$pshome\$($Host.CurrentCulture.Name)"
    
    # Get parameters, examples, full and Online help for a cmdlet or function
    
    # Get a list of all functions
    Get-Command -CommandType Function | 
    Out-GridView -PassThru -Title 'Available functions'
    
    # Get a list of all commandlets
    Get-Command -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available cmdlets'
    
    # Get a list of all functions for the specified name
    Get-Command -Name '*VM*' -CommandType Function | 
    Out-GridView -PassThru -Title 'Available named functions'
    
    # Get a list of all commandlets for the specified name
    Get-Command -Name '*VM**'  -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available named cmdlet'
    
    # get function / cmdlet details
    Get-Command -Name Get-VM -Syntax
    (Get-Command -Name Get-VM).Parameters.Keys
    Get-help -Name Get-VM -Full
    Get-help -Name Get-VM -Online
    Get-help -Name Get-VM -Examples
    
    Get-Command -Name Out-GridView -Syntax
    (Get-Command -Name Out-GridView ).Parameters.Keys
    Get-help -Name Out-GridView -Full
    Get-help -Name Out-GridView -Online
    Get-help -Name Out-GridView -Examples
    
    所以,跳转到Youtube上,在那里搜索使用培训的方法

    你真正的问题是

    …进行字符串搜索将返回一个非常长的样本列表供您使用

    例如:

    MS powershellgallery.com中已经存在为您执行此菜单操作的模块

    Youtube再次用于菜单创建

    您可以下载并用于此类菜单的模块位于PowerShell会话中

    Find-Module -Name '*menu*' | 
    Format-Table -AutoSize
    
    <#
    # Results
    
    Version  Name                     Repository Description                                                                                                                           
    -------  ----                     ---------- -----------                                                                                                                           
    1.0.6    ps-menu                  PSGallery  Powershell module to generate interactive console menu                                                                                
    0.2      MenuShell                PSGallery  Make console menus in seconds with MenuShell                                                                                          
    1.0.52.0 CliMenu                  PSGallery  Easily build and edit CLI menus in Powershell                                                                                         
    0.1.1    ServerOpsMenu            PSGallery  PowerShell module to provide maintenance menu for Windows servers                                                                     
    1.0.1    PSScriptMenuGui          PSGallery  Use a CSV file to make a graphical menu of PowerShell scripts. Easy to customise and fast to launch....                               
    1.0.0.0  SimpleMenu               PSGallery  Create and invoke a simple menu interface.                                                                                            
    2.21     DosInstallUtilities.Menu PSGallery  Functions to create menus                                                                                                             
    1.0.2001 SS.CliMenu               PSGallery  CLI menu infrastructure for PowerShell. ...                                                                                           
    1.0.0.2  MenuSelect               PSGallery  Module description                                                                                                                    
    1.0.4    SLMenu                   PSGallery  Text User Interface Module for Powershell Console                                                                                     
    0.1.1    PSMenu                   PSGallery  Powershell module to generate interactive console menu....                                                                            
    0.5      ContextSensitiveMenus    PSGallery  Allows you to add type-sensitive context menus to WPF controls                                                                        
    
    #>
    
    如果您所做的只是提供一个VM列表供用户选择启动,则不需要太多其他代码。不需要外部文件

    对于MDI方法,您似乎在追求。您仍然可以使用Out GridView进行此操作:

    如果您想变得更加优雅,甚至可以使用内置的PowerShell帮助选项或编写自己的Windows窗体。例如,一个简单的GUI工具,用于VM选择,动态列表填充使用动态参数,用于打开/关闭电源

    function Get-UserVM 
    {
        [cmdletbinding()]
        [Alias('guvm')]  
    
        param
        (
            [ValidateSet('On', 'Off')]
            [Parameter(Mandatory)]
            [string]$State        
        )
    
        DynamicParam 
        {
            # Set the dynamic parameters' name
            $ParameterName = 'VMName'
    
            # Create the dictionary 
            $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
            # Create the collection of attributes
            $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
    
            # Create and set the parameters' attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.Mandatory = $true
            $ParameterAttribute.Position = 1
    
            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)
    
            # Generate and set the ValidateSet 
            $arrSet = ((Get-VM).Name)
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
    
            # Add the ValidateSet to the attributes collection
            $AttributeCollection.Add($ValidateSetAttribute)
    
            # Create and return the dynamic parameter
            $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
            return $RuntimeParameterDictionary
        }
    
    
    
        begin 
        {
            # Bind the parameter to a friendly variable
            $Script:VNName = $PsBoundParameters[$ParameterName]
        }
    
        process 
        {
            switch ($State) 
            { 
                On
                {
                    "Attempting to start $Script:VNName"
                    Start-VM -Name $Script:VNName
                } 
                Off
                {
                    "Attempting to stop $Script:VNName"
                    Stop-VM -Name $Script:VNName
                } 
                default {Write-Warning -Message "$Script:VNName state could not be determined"}
            }    
        }
    }
    
    Show-Command -Name Get-UserVM
    
    # Or
    Show-Command -Name guvm
    
    # Or 
    shcm guvm
    
    然而,这是你的选择。然而,正如我所展示的,你有很多选择,要知道你是否选择了正确的选择,唯一的方法就是先了解它们

    # Show the default Out-GridView as a user menu
    Get-VM -Name '*' | 
    Sort-Object -Property Name | 
    Out-GridView -Title 'Select one or more Windows host to use. CRTL+LeftMouseClick to select more than one' -Passthru | 
    Start-VM -ComputerName $PSItem
    
    function Get-UserVM 
    {
        [cmdletbinding()]
        [Alias('guvm')]  
    
        param
        (
            [ValidateSet('On', 'Off')]
            [Parameter(Mandatory)]
            [string]$State        
        )
    
        DynamicParam 
        {
            # Set the dynamic parameters' name
            $ParameterName = 'VMName'
    
            # Create the dictionary 
            $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
            # Create the collection of attributes
            $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
    
            # Create and set the parameters' attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.Mandatory = $true
            $ParameterAttribute.Position = 1
    
            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)
    
            # Generate and set the ValidateSet 
            $arrSet = ((Get-VM).Name)
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
    
            # Add the ValidateSet to the attributes collection
            $AttributeCollection.Add($ValidateSetAttribute)
    
            # Create and return the dynamic parameter
            $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
            return $RuntimeParameterDictionary
        }
    
    
    
        begin 
        {
            # Bind the parameter to a friendly variable
            $Script:VNName = $PsBoundParameters[$ParameterName]
        }
    
        process 
        {
            switch ($State) 
            { 
                On
                {
                    "Attempting to start $Script:VNName"
                    Start-VM -Name $Script:VNName
                } 
                Off
                {
                    "Attempting to stop $Script:VNName"
                    Stop-VM -Name $Script:VNName
                } 
                default {Write-Warning -Message "$Script:VNName state could not be determined"}
            }    
        }
    }
    
    Show-Command -Name Get-UserVM
    
    # Or
    Show-Command -Name guvm
    
    # Or 
    shcm guvm