小编Cha*_*ler的帖子

将XML转换为PSObject

注意:我正在使用ConvertTo-XML,不能使用Export-Clixml:

我创建一个简单的PSObject:

$a = New-Object PSObject -Property @{
    Name='New'
    Server = $null
    Database = $null
    UserName = $null
    Password = $null
}
Run Code Online (Sandbox Code Playgroud)

然后我使用ConvertTo-XML以下方法将其转换为XML :

$b = $a | Convertto-XML -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

XML看起来像这样:

<?xml version="1.0"?>
<Objects>
  <Object>
    <Property Name="Password" />
    <Property Name="Name">New</Property>
    <Property Name="Server" />
    <Property Name="UserName" />
    <Property Name="Database" />
  </Object>
</Objects>
Run Code Online (Sandbox Code Playgroud)

我无法找出点符号或XPath查询来提取属性/元素并转换$b回原始PSObject.

xml powershell

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

无法在脚本模块中创建PowerShell别名

重现步骤:

使用以下函数和别名在\ WindowsPowerShell\Modules\TestAlias\TestAlias.psm1中创建TestAlias模块:

function foo
{ write-output 'foo' }


New-Alias -name bar -value foo
Run Code Online (Sandbox Code Playgroud)

从PowerShell会话:

import-module TestAlias
bar
Run Code Online (Sandbox Code Playgroud)

术语"bar"不被识别为cmdlet,函数,脚本文件或可操作程序的名称......

powershell

11
推荐指数
2
解决办法
3653
查看次数

无法找到添加类型错误元文件

我创建了一个基本的C#类,它实现了Microsoft.Data.Schema.ScriptDom和Microsoft.Data.Schema.ScriptDom.Sql接口.这两个程序集是Visual Studio Database Edition(VSDB)的一部分,是解析/脚本API.您可以解析SQL文本并输出格式SQL脚本.有关VSDB程序集的更多信息,请参阅此博客文章.由于它们是可再发行,我既包括组件和PowerShell脚本在这里:

#requires -version 2

add-type -path .\Microsoft.Data.Schema.ScriptDom.dll
add-type -path .\Microsoft.Data.Schema.ScriptDom.Sql.dll

$Source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;
using System.IO;

    public class SQLParser
    {
        private IScriptFragment fragment;

        public SQLParser(SqlVersion sqlVersion, bool quotedIdentifier, string inputScript)
        {
            switch (sqlVersion)
            {
                case SqlVersion.Sql80:
                    SQLParser80 (quotedIdentifier, inputScript);
                    break;
                case SqlVersion.Sql90:
                    SQLParser90 (quotedIdentifier, inputScript);
                    break;
                case SqlVersion.Sql100:
                    SQLParser100 (quotedIdentifier, inputScript);
                    break;
            }
        }

        private void SQLParser100 (bool quotedIdentifier, string inputScript)
        { …
Run Code Online (Sandbox Code Playgroud)

c# powershell

4
推荐指数
1
解决办法
5102
查看次数

Dapper 参数长度

不确定这是否与 Dapper 的使用有关。在使用 Dapper 的 SQL Server 上,我看到生成了多个缓存计划,唯一的区别是参数的长度:

(@parentId uniqueidentifier,@childName nvarchar(60)) 
 SELECT [ID] FROM [Items] WHERE [ParentID] = @parentId AND [Name] = @childName

(@parentId uniqueidentifier,@childName nvarchar(91))
SELECT [ID] FROM [Items] WHERE [ParentID] = @parentId AND [Name] = @childName

(@parentId uniqueidentifier,@childName nvarchar(15))
 SELECT [ID] FROM [Items] WHERE [ParentID] = @parentId AND [Name] = @childName
Run Code Online (Sandbox Code Playgroud)

是否有任何控制参数长度的 Dapper 配置设置?是否可以设置为nvarchar(256)与表列定义匹配的固定长度?

t-sql sql-server dapper

4
推荐指数
1
解决办法
705
查看次数

Powershell PSReadLine 历史记录

我对 Powershell 中的跨会话历史记录如何工作感到困惑PSReadLine。我可以看到在 PS 版本 5.1 中我以前的命令历史记录自动存储在

%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\
Run Code Online (Sandbox Code Playgroud)

但是,如果我运行,get-history我只能看到当前的会话命令历史记录。我认为在更高版本的 Powershell 中不再需要用于跨会话保存命令历史记录的钩子。我缺少什么?

powershell

4
推荐指数
1
解决办法
8145
查看次数

Powershell V2 外部 MAML 帮助

我正在尝试为脚本模块创建外部 MAML 帮助文件。作为测试,我创建了一个名为“ModTest”的简单模块,其中包含 2 个保存在 .psm1 文件中的函数:

function Test-SqlScript2 
{
}
function Out-SqlScript2
{
}
Run Code Online (Sandbox Code Playgroud)

我将模块保存在我的用户模块目录 ~\Documents\Modules\ModTest 接下来我为 MAML 文件创建了一个子目录 ~\Documents\Modules\ModTest\en-US 我用于测试的 MAML 文件可在此处获得。然后我启动了 PowerShell 并使用 Import-Module 来导入模块。

与已编译的 cmdlet 不同,文件的位置本身不起作用

所以,接下来我尝试将帮助链接添加到脚本模块的顶部,这也不起作用:

<#
.ExternalHelp C:\Users\cmiller6\Documents\WindowsPowershell\Modules\ModTest\en-US\ModTest.help.xml 
#>


function Test-SqlScript2 
{
}
function Out-SqlScript2
{
Run Code Online (Sandbox Code Playgroud)

然后我尝试将帮助信息添加到每个函数中,这确实有效:

function Test-SqlScript2 
{
<#
.ExternalHelp C:\Users\cmiller6\Documents\WindowsPowershell\Modules\ModTest\en-US\ModTest.help.xml 
#>
}
function Out-SqlScript2
{
<#
.ExternalHelp C:\Users\cmiller6\Documents\WindowsPowershell\Modules\ModTest\en-US\ModTest.help.xml 
#>
Run Code Online (Sandbox Code Playgroud)

两个问题:

  1. 是否可以创建脚本模块级别的外部 MAML 帮助,或者您是否需要在每个函数中指定帮助链接?
  2. 尽管文档声明和博客文章表明在指定路径 (~/ModTest\ModTest.help.xml) 时将自动搜索特定于语言的文件夹,即 en-US,但我无法解析 MAML 文件,除非我包含显式路径(~/ModTest/en-US/ModTest.help.xml)。这是一个错误吗?有关 get-help 和特定语言文件夹的文档,请参阅以下链接:

为 Windows PowerShell 模块编写帮助 PowerShell V2 外部 …

powershell

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

标签 统计

powershell ×5

c# ×1

dapper ×1

sql-server ×1

t-sql ×1

xml ×1