Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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正则表达式产生与.NET online tester不同的结果_Powershell - Fatal编程技术网

Powershell正则表达式产生与.NET online tester不同的结果

Powershell正则表达式产生与.NET online tester不同的结果,powershell,Powershell,我正在尝试从文件中读取参数并自动将其分配给powershell中的变量。但在powershell中使用正则表达式时,与使用相应的.NET正则表达式风格相比,我得到了不同的输出 以下是我的输入文件(install.ini)的内容: [sqlinstall] 开关参数 ------------------ Active=“Off” Install=“false” Update=“false” Config=“false” Local=“false” 新变量=“测试变量” 路径参数 ---------

我正在尝试从文件中读取参数并自动将其分配给powershell中的变量。但在powershell中使用正则表达式时,与使用相应的.NET正则表达式风格相比,我得到了不同的输出

以下是我的输入文件(install.ini)的内容:

[sqlinstall]
开关参数
------------------
Active=“Off”
Install=“false”
Update=“false”
Config=“false”
Local=“false”
新变量=“测试变量”
路径参数
----------------
InstallPath=“C:\”
我使用以下powershell脚本(extract.ps1)提取了内容:

设置位置$PSScriptRoot
$checkfile=“install.ini”
$readfile=获取内容$checkfile
$filecontent=“$readfile”
#REGEX FILTER Install.ini
# -------------------
#获取所有“switchname=switchvalue”
$regex_equals='(?m)(?\w+(=)\s*=\s*“(?\w+|*”)
#获取所有文件夹路径:“路径名=C:\或C:\文件夹\filde.pdf”
$regex\u path='(?m)(?\w+?=)\s*=\s*\'?(?\w:\\*[\\]{1}.*\w | \w:\\)'
#获取所有内容:'Parameter=Fullpath、Filepath、Filename、ext'
#$regex=“(?\w+?=)\s*=\s*\”?(?(?\w:\*[\\]{1}.*\”?)\(?\w+\(?\w{2,3}))”
$options=@('MultiLine')#[Text.RegularExpressions.RegexOptions]'MultiLine'
#读取Install.ini('switchname=switchvalue')
#---------------------------------------------
#[regex]::匹配($filecontent,$regex_equals,$options)
$null=[regex]::匹配($filecontent,$regex_等于,$options)|%{$\.Groups[1].Value}-OutVariable switchname
$null=[regex]::匹配($filecontent,$regex_等于,$options)|%{$\.Groups[2].Value}-OutVariable-switchvalue
#读取Install.ini('pathname=C:\或C:\ FOLDER\filde.pdf')
#---------------------------------------------
#[regex]::匹配($filecontent、$regex_path、$options)
$null=[regex]::匹配($filecontent,$regex_path,$options)|%{$_.Groups[1].Value}-OutVariable路径名
$null=[regex]::匹配($filecontent,$regex_path,$options)|%{$_.Groups[2].Value}-OutVariable pathvalue
#将文件参数关联到变量
#-------------------------------------
$dollar=“$”
$mark=“”
$Active=“Off”
$Install=“false”
$Update=“false”
$Config=“false”
$Local=“false”
$New_Variable=“测试输入”
#>
foreach($switchname中的名称){
对于($i=0;$i-lt$switchname.Count;$i++){
交换机($switchname[$i]){
($name){New Variable-name$name-Value$switchvalue[$i]}
}
} 
}
#('pathname=C:\或C:\ FOLDER\filde.pdf')=>$InstallPath=“C:\”
foreach($pathname中的名称){
对于($i=0;$i-lt$pathname.Count;$i++){
交换机($pathname[$i]){
($name){New Variable-name$name-Value$pathvalue[$i]}
}
} 
}
在线测试regex时,两种模式均未显示错误:

Powershell显示大多数变量的正确结果:

------
输入:
------
写入主机$Active
写入主机$Install
写入主机$Update
写入主机$Config
写入主机$Local
写入主机$InstallPath
------
输出:
------ 
关
假的
假的
假的
假的
C:\
但它并不能为所有变量生成正确的输出:

------
输入:
-----
写入主机$New\u变量
------
输出:
------
测试变量“路径参数-----------------InstallPath=“C:\

也许我把多行选项放错了?非常感谢您的建议。

使用合适的Powershell数据文件怎么样?它易于阅读,易于创建或操作,甚至易于加载和使用

您的示例参数文件如下所示:

@{
    SwitchParameter = @{
        Active       = 'Off'
        Install      = $true 
        Update       = $false
        Config       = $false
        Local        = $false
        New_Variable = 'Test Variable'
    }
    PathParametres = @{
        InstallPath = 'C:\'
    }
}
$Params = Import-PowerShellDataFile -Path .\install.psd1
$Params.SwitchParameter.Install
#or 
$Params.PathParametres.InstallPath
假设您将其命名为
install.psd1
,您可以这样导入它:

@{
    SwitchParameter = @{
        Active       = 'Off'
        Install      = $true 
        Update       = $false
        Config       = $false
        Local        = $false
        New_Variable = 'Test Variable'
    }
    PathParametres = @{
        InstallPath = 'C:\'
    }
}
$Params = Import-PowerShellDataFile -Path .\install.psd1
$Params.SwitchParameter.Install
#or 
$Params.PathParametres.InstallPath
现在您可以使用包含的变量,如下所示:

@{
    SwitchParameter = @{
        Active       = 'Off'
        Install      = $true 
        Update       = $false
        Config       = $false
        Local        = $false
        New_Variable = 'Test Variable'
    }
    PathParametres = @{
        InstallPath = 'C:\'
    }
}
$Params = Import-PowerShellDataFile -Path .\install.psd1
$Params.SwitchParameter.Install
#or 
$Params.PathParametres.InstallPath

这将比使用字符串和正则表达式杂技更简单、更专业

您的ini文件看起来有点奇怪,因为它包含不应该出现在ini文件中的字符串和分隔线

无论如何,使用helper函数,您可以解析文件以接收(有序)哈希表,其中包含文件的属性:

function Parse-Ini {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        $Content
    )

    # PowerShell < 3.0
    # $ini = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
    $ini     = [ordered]@{}
    $section = "UNDEFINED"
    $line    = 0

    $Content | ForEach-Object {
        $line++
        switch -regex ($_) {
            # a section
            '^\s*\[(.+)\]\s*$' {
                $section = $matches[1].Trim()
                if (!($ini[$section])) { $ini[$section] = [ordered]@{} }
                break
            }

            # a property: Key = Value
            '^\s*([^#]+?)\s*=\s*(.*)' {
                if (!($ini[$section])) { $ini[$section] = [ordered]@{} }
                # remove inline comment, trim and unquote
                $name, $value = ($matches[1..2] -replace '#.*$').Trim() -replace '^(?:"(.*)"|''(.*)'')$', '$1$2'
                $ini[$section][$name] = $value
                break
            }

            # skip comment and empty or whitespace only lines
            '^\s*#?$' { }

            # bad entry
            default {
                # comment out the next line if you don't want to seen the warnings
                Write-Warning "Bad entry detected in line $($line): '$_'"
            }
        }
    }

    return $ini
}
它获取一个哈希表,其中包含一个条目
sqlinstall
,该条目包含另一个哈希表,该哈希表包含您要查找的所有属性:

$Params.sqlinstall
要获得不同的条目,只需使用点符号,如

$installPath = $Params.sqlinstall.InstallPath

由于前面提到的无效条目,函数将发出警告消息。如果您不关心这些消息,只需注释掉行
Write Warning“在第$($line)行中检测到错误条目:'$.'

Olaf.。如果我有一个爱代码的心表情符号,我会给您发一个:)那太好了!非常感谢你的帮助。我会的,只要我再次通过我的“stackoverflow新手”状态,即15个声誉lol。。非常感谢。这太棒了,正是我想要实现的!谢谢你,提奥!工作完美。知道我的正则表达式为什么不起作用吗?@HectorEduNseng说实话,我没有尝试你的正则表达式代码,因为它看起来太复杂了,而且只用于处理这一个文件的内容。我看到的是一个ini文件(虽然中间有一些无效的行),可以用更通用的方式处理。很高兴你喜欢我的回答@HectorEduNseng我刚刚用更好的代码更新了函数,以删除内联注释,修剪并取消引用条目的名称和值。享受!:))我真的为你的密码感到高兴西奥!非常感谢!