小编mah*_*esh的帖子

将冒号分隔的字符串转换为 PowerShell 字典

我正在尝试将冒号分隔字符串转换为 PowerShell 字典。以下是字符串。

$inputkeyvalues = "Appsetting:true|environment:prod"
Run Code Online (Sandbox Code Playgroud)

我在$inputkeyvalues变量中有两个键值对,它们由管道分隔符分隔。第一个是:Appsetting:true 第二个是:environment:prod

我正在尝试转换为 PowerShell 字典。最终输出应该是这样的,

Key            Value
-----         -----
Appsetting     true
environment    prod

$Dictionary= New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
Run Code Online (Sandbox Code Playgroud)

有人可以为此建议我可能的解决方案。提前致谢。

powershell powershell-2.0

6
推荐指数
1
解决办法
4736
查看次数

提取两个字符powershell之间的所有字符串

以下是两个字符.1. {{2.}}我试图在以下文件中找到这些字符之间的所有字符串.

    <add key="CSDFG_SqlServerFailOverInterval" value="{{environment}}"/>
    <add key="CSDFG_JobResetInterval" value="600000"/>
    <add key="FileTypeFilter" value="{{filetype}}"/>
Run Code Online (Sandbox Code Playgroud)

我正在使用以下powershell命令,

$x = C:\app.config
$s = Get-Content $x
$prog = [regex]::match($s,'{{([^/)]+)}}').Groups[1].Value
write-host $prog
Run Code Online (Sandbox Code Playgroud)

我的输出只是一个字符串.它并没有拉动所有的弦.有人可以建议我如何获得所有的字符串.提前致谢.

powershell powershell-2.0

5
推荐指数
1
解决办法
1万
查看次数

借助PowerShell词典更新web.config中的应用程序设置

我正在尝试使用PowerShell更改web.config文件中的应用程序设置

以下是web.config文件;

<configuration>
    <connectionStrings>
        <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <appSettings>
        <add key="ActivePeriod" value="false" />
        <add key="Environment" value="UAT" />
        <add key="authmode" value="4" />
        <add key="IsEncryptedConfig" value="true" />
        <add key="LogErrorsToText" value="true" />
    </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我想更改应用程序设置值。为此,我将所有相应的值存储在PowerShell字典中。这是我的字典。

Key                     Value
-----                   -----
ActivePeriod            true
Environment             prod
LogErrorsToText         false
Run Code Online (Sandbox Code Playgroud)

现在,我想将每个Dictionary键与appsetting键进行匹配。如果任何字典键与appsetting键匹配,则应替换相应的值。就我而言,我期望以下输出;

<configuration>
    <connectionStrings>
        <add name="TestDBEntities" connectionString="metadata=res://*/TestProject.csdl|res://*/TestProject.ssdl|res://*/TestProject.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQL01;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <appSettings>
        <add key="ActivePeriod" value="true" />
        <add key="Environment" value="prod" />
        <add key="authmode" value="4" />
        <add key="IsEncryptedConfig" value="true" /> …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0

2
推荐指数
1
解决办法
4464
查看次数

在PowerShell中过滤以(一或两个)斜杠开头的字符串

我试图找出以一个斜杠(/)和两个斜杠(//)开头的字符串。例如,以下是具有几个字符串的数组:以下是我正在尝试的代码:

$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
    if ($string.StartsWith("/"))
    {
        Write-Host "$string has one slash."
    }
    elseif($string.StartsWith("//"))
    {
        Write-Host "$string has two slashes."
    }
    else
    {
        #I want to exit only when below conditions meet
        #1. if string doesnot have any slash or
        #2. if string has more than two slashes
        Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
        Exit -1
    }
}
Run Code Online (Sandbox Code Playgroud)

我不想写更多的if条件来过滤这些东西,但这不能按预期工作。我认为我应该更改逻辑以达到要求。有人可以建议我吗(我正在寻找动态方法)

regex powershell filter powershell-2.0

0
推荐指数
1
解决办法
2836
查看次数

标签 统计

powershell ×4

powershell-2.0 ×4

filter ×1

regex ×1