不同的PowerShell文件类型有哪些?

18

如果我尝试在 ISE 中保存文件,它默认的扩展名是带有括号和描述的 Powershell Files (*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml)。

Powershell Scripts (*.ps1)
Powershell Modules (*.psm1)
Powershell Data Files (*.psd1)
Powershell Session Configuration Files (*.pssc)
Powershell Role Capability Files (*.psrc)
Powershell Xml Files (*.psxml, *.cdxml)

这些各自是做什么的?

1个回答

27
  • .ps1文件是PowerShell脚本,是最常见的PowerShell文件类型,也是最像其他shell脚本如.sh.bat文件的类型。

  • .psm1文件是PowerShell模块,可以包含多个函数脚本作为一个包一起交互,可以使用Import-ModuleInstall-Module cmdlet将模块导入到PowerShell中。

    • 其他模块交互cmdlet:
      • Export-ModuleMember
      • Find-Module
      • Get-InstalledModule
      • Get-Module
      • ImportSystemModules
      • New-Module
      • New-ModuleManifest
      • 等,在PowerShell中通过*Module*进行查找。
  • .psd1文件是PowerShell数据文件或PowerShell模块清单文件,提供信息和指令,作为psm1文件/PowerShell模块的清单文件,存储配置数据。可以作为PowerShell模块的帮助文件。有关更多信息,请参见https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest?view=powershell-7。使用Import-PowerShellDataFileNew-ModuleManifestTest-ModuleManifestUpdate-ModuleManifestImport-LocalizedData cmdlet进行操作。

  • .ps1xml文件是PowerShell模块的PowerShell XML,类似于.psd1文件,但格式为XML。需要模块清单文件才能作为模块的帮助文件。有关ps1xml文件的某些名称指定要执行的某些操作,请参见https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_types.ps1xml?view=powershell-7https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_format.ps1xml?view=powershell-7

  • .pssc文件是PowerShell会话配置文件;描述会话配置文件,用于定义使用会话配置的会话的环境(也称为“终结点”)。会话配置文件使得创建自定义会话配置变得容易,而无需使用复杂的C#程序集或脚本。 .pssc文件采用哈希表格式并包含PowerShell会话的配置信息。请参见https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_session_configuration_files?view=powershell-7。使用Register-PSSessionConfigurationSet-PSSessionConfigurationNew-PSSessionConfigurationFile cmdlet进行操作。

  • .psrc文件是PowerShell角色能力文件;它们定义了在会话配置和会话配置(pssc)文件中使用的一组能力。采用哈希表格式。使用New-PSRoleCapabilityFile cmdlet。请参见https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-psrolecapabilityfile?view=powershell-7

  • .cdxml文件是使用CIM工具创建cmdlet的PowerShell XML文件。请参见https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/cdxml-overview

    示例:

    示例 psd1:

    @{
    
    RootModule = 'ExampleModule.psm1'
    ModuleVersion = '0.0.1'
    GUID = '1234abcd-1234-abcd-xxxx-000000000000'
    Author = 'User01'
    CompanyName = 'Unknown'
    Copyright = '(c) User01. All rights reserved.'
    Description = 'Example module'
    

    例如types.ps1xml

    <Type>
      <Name>System.Object[]</Name>
      <Members>
        <AliasProperty>
          <Name>Test</Name>
          <ReferencedMemberName>
            Length
          </ReferencedMemberName>
        </AliasProperty>
      </Members>
    </Type>
    

    将成员类型为AliasProperty的新成员Count添加到System.Array中:

    gm -InputObject @(1,2,3,4)| ?{$_.Name -eq "test"}
    
    
       TypeName: System.Object[]
    
    Name       MemberType    Definition
    ----       ----------    ----------
    Test       AliasProperty Test = Length
    

    示例 psrc:

    New-PSRoleCapabilityFile `
        -Path ".\Test.psrc" `
        -Author "TestUser" `
        -CompanyName "Test Company" `
        -Description "Description" `
        -ModulesToImport "Microsoft.PowerShell.Core"
    

    创建带参数的Test.psrc文件
    Test.psrc文件内容:
    @{
    
    # ID used to uniquely identify this document
    GUID = '4fb9608e-474c-44c3-a752-ea1d9b27dcb0'
    
    # Author of this document
    Author = 'TestUser'
    
    # Description of the functionality provided by these settings
    Description = 'Description'
    
    # Company associated with this document
    CompanyName = 'Test Company'
    
    # Copyright statement for this document
    Copyright = '(c) 2020 TestUser. All rights reserved.'
    
    # Modules to import when applied to a session
    ModulesToImport = 'Microsoft.PowerShell.Core'
    
    # Aliases to make visible when applied to a session
    # VisibleAliases = 'Item1', 'Item2'
    
    # Cmdlets to make visible when applied to a session
    # VisibleCmdlets = 'Invoke-Cmdlet1', @{ Name = 'Invoke-Cmdlet2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }
    
    # Functions to make visible when applied to a session
    # VisibleFunctions = 'Invoke-Function1', @{ Name = 'Invoke-Function2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }
    
    # External commands (scripts and applications) to make visible when applied to a session
    # VisibleExternalCommands = 'Item1', 'Item2'
    
    # Providers to make visible when applied to a session
    # VisibleProviders = 'Item1', 'Item2'
    
    # Scripts to run when applied to a session
    # ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'
    
    # Aliases to be defined when applied to a session
    # AliasDefinitions = @{ Name = 'Alias1'; Value = 'Invoke-Alias1'}, @{ Name = 'Alias2'; Value = 'Invoke-Alias2'}
    
    # Functions to define when applied to a session
    # FunctionDefinitions = @{ Name = 'MyFunction'; ScriptBlock = { param($MyInput) $MyInput } }
    
    # Variables to define when applied to a session
    # VariableDefinitions = @{ Name = 'Variable1'; Value = { 'Dynamic' + 'InitialValue' } }, @{ Name = 'Variable2'; Value = 'StaticInitialValue' }
    
    # Environment variables to define when applied to a session
    # EnvironmentVariables = @{ Variable1 = 'Value1'; Variable2 = 'Value2' }
    
    
    # Format files (.ps1xml) to load when applied to a session
    # FormatsToProcess = 'C:\ConfigData\MyFormats.ps1xml', 'C:\ConfigData\OtherFormats.ps1xml'
    
    # Assemblies to load when applied to a session
    # AssembliesToLoad = 'System.Web', 'System.OtherAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    
    }
    

    例子 {{pssc}}

    New-PSSessionConfigurationFile -RoleDefinitions
    @{ 'User' = @{ RoleCapabilities = 'Maintenance' }}
    -Path .\Maintenance.pssc
    

    创建 Maintenance.pssc 并添加以下内容:

    @{
    
    # Version number of the schema used for this document
    SchemaVersion = '2.0.0.0'
    
    # ID used to uniquely identify this document
    GUID = 'a4f7e8eb-f787-4f2c-bf0f-555db2ca2b05'
    
    # Author of this document
    Author = 'User'
    
    # Description of the functionality provided by these settings
    # Description = ''
    
    # Session type defaults to apply for this session configuration. Can be 'RestrictedRemoteServer' (recommended), 'Empty', or 'Default'
    SessionType = 'Default'
    
    # Directory to place session transcripts for this session configuration
    # TranscriptDirectory = 'C:\Transcripts\'
    
    # Whether to run this session configuration as the machine's (virtual) administrator account
    # RunAsVirtualAccount = $true
    
    # Scripts to run when applied to a session
    # ScriptsToProcess = 'C:\ConfigData\InitScript1.ps1', 'C:\ConfigData\InitScript2.ps1'
    
    # User roles (security groups), and the role capabilities that should be applied to them when applied to a session
    RoleDefinitions = @{
        'User' = @{
            'RoleCapabilities' = 'Maintenance' } }
    
    }
    

    使用{{注册文件}}进行注册

    Register-PSSessionConfiguration -Name Name -Path .\Maintenance.pssc
    

    现在使用Name启动的PowerShell会话使用Maintenance.pssc。要使用Name开始一个PowerShell会话,请使用{{命令}}。
    New-PSSession -ConfigurationName Name
    

2
非常好的回复。感谢您为社区提供了强有力、全面的贡献! - rbleattler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接